Total Pageviews

Donate

Dec 29, 2010

Python String Literals

Some times while dealing with strings using python we find that a string starts with a character before the string quotes. for example:
r'here is my string' 
or:
u'here is another string'
what are the 'r' and 'u'??

In python strings, we may use the backslash (\) character to escape characters that has different meanings such as:

  • New Line (\n)
  • Tab (\t)
  • backslash it self (\\) :)
  • ...etc

Also strings may optionally be prefixed with a letter - like the examples above - and here are there different meanings:

  • A prefix letter 'r' or 'R', these strings are called "Raw Strings" and they use different rules for the backslash skipping rule ...
    • All characters following the backslash are included in the string as they are. For example "\n" is not a new line ... it is a backslash followed by a small character 'n'. Also, "\"" will give a string error because the backslash followed by a double quotes is interpreted as they are and hence, this string contains three double quotes and this is invalid. 
    • Also if a single backslash followed by a newline is interpreted as two characters as a part of the string, not as a line continuation !!
  • A prefix letter 'u' or 'U' makes it a "Unicode String" which used the Unicode character set defined by the Unicode Consortium ISO 10646.
    • Here the backslash only escape the codes for characters. Let's see examples:
      • u"\u0062" is a small letter 'b'.
      • u"\u0025" is the percentage sign '%'
      • u"\n" here the backslash will not escape the new line character and the string will remain as it is "\n"  
Hope this was useful :)

2 comments:

  1. thx for that valuable posts...
    can you please get over a post where parsing XML files using mod python
    Thx.

    ReplyDelete

Donate