Escape Sequences and Raw Strings in Python

In the previous article we saw some string programs to demonstrate common characteristics of Strings in Python, now let us examine the concept of escape sequences and raw strings in Python. Imagine the below statement as part of a program

s= 'Welcome to Codekyro's Course'
print(s)
Output:
  File "main.py", line 1
    s= 'Welcome to Codekyro's Course'
                        ^
SyntaxError: invalid syntax

The program fails to execute simply because the apostrophe interferes with the program’s syntax where a quote symbol is recognized as the end of the print statement. However, there may be legitimate reasons or cases when the apostrophe is necessary to include within statements. How can we deal with such scenarios?

s='Welcome to Geek\'s Course'
print(s)
Output:
Welcome to Geek's Course

The program now executes as we expect, without any issues as we use an escape sequence. Escape sequences can avoid the limits between the usual double quote statement. We use a backslash character and then the character that we want to escape. So what happens in the above program is that the backslash specified allows Python to ‘escape’ the single quote(apostrophe), so it is no longer treated as the end of the print statement

Various Escape Sequences and Raw Strings in Python

/n: It is used to define a new line, hence when this escape sequence is included within a statement. The segment or the portion of the statement after it is no longer treated as part of the same string or statement but as a new line.
s="Hi, \n Welcome to the Course"
print(s)
Output:
Hi, 
Welcome to the Course

We can also escape the backslash sequences, backslash followed by certain characters forms an escape sequence similar to \n. Suppose it is a backslash statement not followed by any of the specific characters that would make an escape sequence it would work fine.
We can escape the backslash sequence by adding an additional backslash. For example:

s4="Line with single \n backslash"
print(s4)
s3="Line with double \\n backslash"
print(s3)
Output:
Line with single 
backslash
Line with double \n backslash

\t: It prints a tab, which is essentially 4 spaces of area, it is useful for indenting purposes.

Raw Strings

Let us understand the use of raw strings in Python:

s1= "C:\project\name.py"
s2=r"C:\project\name.py"
print(s1)
print(s2)
Output:
C:\project
ame.py
C:\project\name.py

Raw Strings are another conceivable alternative way of dealing with escape sequences in Python, in the above program in s1, the output executes according to the \n escape sequence present between the double quotes in the file path(C:\project\name.py)). In s2 however, it executes as it should because the raw string is included. Raw String can be used by mentioning small r or capitalized R before the starting double quote. The escape sequences in a string that includes the r raw string declaration are not processed.

Hence we have seen the utilization of escape sequences and raw strings in Python, next we will look at Formatted Strings in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *