Certified Python Developer Learning Resources Escape Sequence and string concatenation and format method

Learning Resources

Escape Sequence and string concatenation and format method

Escape Sequences
Suppose, you want to have a string which contains a single quote ('), how will you specify this string? For example, the string is What's your name?. You cannot specify 'What's your name?' because Python will be confused as to where the string starts and ends. So, you will have to specify that this single quote does not indicate the end of the string. This can be done with the help of what is called an escape sequence. You specify the single quote as \' - notice the backslash. Now, you can specify the string as 'What\'s your name?'.

Another way of specifying this specific string would be "What's your name?" i.e. using double quotes. Similarly, you have to use an escape sequence for using a double quote itself in a double quoted string. Also, you have to indicate the backslash itself using the escape sequence \\.

What if you wanted to specify a two-line string? One way is to use a triple-quoted string as shown previously or you can use an escape sequence for the newline character - \n to indicate the start of a new line. An example is This is the first line\nThis is the second line. Another useful escape sequence to know is the tab - \t. There are many more escape sequences but I have mentioned only the most useful ones here.

One thing to note is that in a string, a single backslash at the end of the line indicates that the string is continued in the next line, but no newline is added. For example:

   "This is the first sentence. \
   This is the second sentence."

is equivalent to "This is the first sentence. This is the second sentence.".

Raw Strings
If you need to specify some strings where no special processing such as escape sequences are handled, then what you need is to specify a raw string by prefixing r or R to the string. An example is r"Newlines are indicated by \n".

Strings Are Immutable
This means that once you have created a string, you cannot change it. Although this might seem like a bad thing, it really isn't. We will see why this is not a limitation in the various programs that we see later on.

String Literal Concatenation
If you place two string literals side by side, they are automatically concatenated by Python. For example, 'What\'s ' 'your name?' is automatically converted into "What's your name?".

Note for C/C++ Programmers - There is no separate char data type in Python. There is no real need for it and I am sure you won't miss it.

Note for Perl/PHP Programmers - Remember that single-quoted strings and double-quoted strings are the same - they do not differ in any way.

Note for Regular Expression Users - Always use raw strings when dealing with regular expressions. Otherwise, a lot of backwhacking may be required. For example, backreferences can be referred to as '\\1' or r'\1'.

The format Method -

Sometimes we may want to construct strings from other information. This is where the format() method is useful.

# Filename: str_format.py
age = 26
name = 'Swaroop'
print('{0} is {1} years old'.format(name, age))
print('Why is {0} playing with that python?'.format(name))


   $ python str_format.py
   Swaroop is 26 years old
   Why is Swaroop playing with that python?

How It Works:
A string can use certain specifications and subsequently, the format method can be called to substitute those specifications with corresponding arguments to the format method.

Observe the first usage where we use {0} and this corresponds to the variable name which is the first argument to the format method. Similarly, the second specification is {1} corresponding to age which is the second argument to the format method.

Notice that we could have achieved the same using string concatenation: name + ' is ' + str(age) + ' years old' but notice how much uglier and error-prone this is. Second, the conversion to string would be done automatically by the format method instead of the explicit conversion here. Third, when using the format method, we can change the message without having to deal with the variables used and vice-versa.

What Python does in the format method is that it substitutes each argument value into the place of the specification. There can be more detailed specifications such as:

>>> '{0:.3}'.format(1/3) # decimal (.) precision of 3 for float
>>> '{0:_^11}'.format('hello') # fill with underscores (_) with the text centered (^) to 11 width
>>> '{name} wrote {book}'.format(name='Swaroop', book='A Byte of Python') # keyword-based
'Swaroop wrote A Byte of Python'

 For Support