Python Tutorial #2

What exactly u and r – string flag means?

In Python 2, you can store the string in 2 different types.

The first one is ASCII which is str type in python, it uses 1 byte of memory. (256 characters, will store mostly English alphabets and simple symbols)

The 2nd type is UNICODE which is unicode type in python, it uses 2 bytes of memory. (65536 characters, so this include all characters of all languages on earth)

By default, python will prefer str type but if you want to store the string in unicode type you can put u in front of the text like u’text’ or you can do this by calling unicode(‘text’)

So u is just a short way to call a function to cast str to unicode. That’s it!

Now the r part, you put it in front of the text to tell the computer that the text is raw text, backslash should not be an escaping character. r’\n’ will not create a new line character. It’s just plain text containing 2 characters where first character is ‘\’ and second character is ‘n’

In [68]: a=’qmi\nasd’

In [69]: print(a)

In [70]: a=r’qmi\nasd’

In [71]: print(a)

If you want to convert str to unicode and also put raw text in there, use ur because ru will raise an error.

NOW, the important part:

You cannot store one backslash by using r, it’s the only exception. So this code will produce an error: r’\’

To store a backslash (only one) you need to use ‘\’

If you want to store more than 1 characters you can still use r like r’\’ will produce 2 backslashes as you expected.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s