The Python Reference Guide includes several string literals that you can use in a string. These special character sequences are replaced by the intended escape sequence value.
Here is a table of some of the most useful escape sequences and a description of their output.
Escape Sequence Meaning \t Tab \\ Inserts a back slash (\) \' Inserts a single quote (') \" Inserts a double quote (") \n Inserts a ASCII Linefeed (a new line)
Basic example
If I wanted to print some data points separated by a space, I could print this line.
DataString = "0\t12\t24" print (DataString)
Returns
0 12 24
List example
Here is another example where we print list items and we want to separate items using TAB.
DataPoints = [0,12,24] print (str(DataPoints[0]) + "\t" + str(DataPoints[1]) + "\t" + str(DataPoints[2]))
Returns
0 12 24
Raw lines
Note that raw strings (a string containing the prefix "r"), string literals will be ignored. This allows you to include these special character sequences in strings without modification.
DataString = r"0\t12\t24" print (DataString)
Returns
0\t12\t24
What could be an undesirable conclusion
Line length
It should also be noted that string literals are only one character long.
DataString = "0\t12\t24" print (len(DataString))
Returns
7
The raw string has a length of 9.
CodeCupboard Apr 13 '17 at 11:21 on 2017-04-13 11:21
source share