Category: Python

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.



Python Tutorial #1

Here are some of the points which are important to me.

Regular expressions:

.*? means “match zero or more characters but as little as possible“. It is called a non-greedy match.

.* means almost the same except that as much as possible is matched (greedy match).


  • Using f.*?a to match foo bar baz results in foo ba (stops after first a)
  • Using f.*a to match foo bar baz results in foo bar ba (stops after last a)

In Python regular expression, greedy means as much as possible.

Numeric Operator:

integer division,  a//b

float division,  a/b

Split Function: Separate two inputs with space by split function

i, c = input().split()

Difference between lists and tuples in python?

We can use tuples as

  1. keys in dictionaries
  2. elements of sets

whereas lists can not.