Python. Strings. Escape-sequences. Unformatted strings. Multiline text blocks




Strings. Escape-sequences. Unformatted strings. Multiline text blocks


Contents


Search other websites:

1. General form of representing an escape-sequence. The \ (backslash) character.

Escape-sequences are sequences of characters that define special characters that are hard to type on the keyboard or display on the screen. Such characters include, for example, a newline character, a PC Speaker beep character, a BackSpace key character, and more.

To represent special characters, use the \ character. The general form of the escape-sequence is as follows:

\characters

here characters – some characters or numbers that replace one character. This character has a binary value defined by an escape-sequence.

 

2. Examples of output strings containing sequences screened

Escape-sequences affect lines output.

# Escape-sequences
import math

s = 'H\ne\nl\nl\no\nw\to\tr\tl\td\n'
s2 = '\x23+\x25=\x28'
print(s)
print(s2)

After executing the above code, the following result will be obtained

H
e
l
l
o
w   o    r    l    d

3+5=8

 

3. The table of escape-sequences

The table shows the escape-sequences of the Python language.

Python. The table of escape sequences

 

4. Determining the length of a string containing an escaped sequence

In a program, an escape-sequence may consist of several characters. When displayed, these few characters are replaced with one corresponding character.

Example.

# Escape sequences
import math

s = 'A\n\172' # \172 - character code Z
length = len(s) # length = 3
print(s)
print('length = ',length)

In the above code, the line

s = 'A\n\172'

in the program has a length of 7 characters. The actual string length is 3 characters. This is because the string contains two escaped sequences that are replaced by single characters:

  • \n – newline character;
  • \172 – character code z in octal number system.

The result of the program

A
z
length = 3


 

5. Representation of characters in hexadecimal and octal systems. Example

Using escaped sequences, you can represent characters in hexadecimal and octal systems.

Example.

# Escape sequences
# hexadecimal number system
s1 = '\xF5A0' # s1 = '\xf5A0'
l1 = len(s1) # l1 = 3 => { '\xf5', 'A', '0' }

# octal number system
s2 = '\562' # s2 = 'r' - symbol in octal system
l2 = len(s2) # l2 = 1

print('s1 = ', s1)
print('l1 = ', l1)
print('s2 = ', s2)
print('l2 = ', l2)

The result of the program

s1 = '\xf5A0'
l1 = 3
s2 = 'r'
l2 = 1

 

6. The representation of the backslash character \. Example

The backslash character \ is stored in the string as it is if there is no valid service character after the \ character that is displayed in the table in section 3.

Example.

# Escape sequences
# backslash character \
s = '\error' # s = \error
s = "12\3562" # s = 12o2, \356 => o
s = 'c:\\myfile.txt' # s = 'c:\myfile.txt', \\=>\
s = 'c:\myfile.txt' # s = 'c:\myfile.txt'

 

7. Disabling the escape-sequences mechanism. Example

When using escaped sequences, it is possible that using the \ (backslash) character in combination with other characters generates invisible errors.

For example, if in some variable s you need to write the full name of the file, which is placed along the following path

C:\1\textfile.txt

then a string in Python

s = 'C:\1\textfile.txt' # s = 'C:    extfile.txt'

will contain an invisible error: the variable s is equal to the value

C:    extfile.txt

This is due to the fact that the sequence of characters \t is considered as one tab character (see the table with p. 3).

In order to avoid such negative cases, you need to disable (suppress) the mechanism of escape-sequences. The following general form is used to disable escape sequences in strings.

r'character_string'

or

R'character_string'

that is, before the string you need to put the character r or R.

Example.

# Escape sequences. Invisible errors
# 1. Wrong code
s = 'C:\1\textfile.txt' # s = 'C:    extfile.txt'
l = len(s) # l = 15

# 2. Disabling escape sequences: r or R
# Correct code
s2 = R'C:\1\textfile.txt' # s2 = 'C:\1\textfile.txt'
l2 = len(s2)

print(s)
print('l = ', l)
print(s2)
print('l2 = ', l2)

As you can see from the example, an erroneous full file name is generated in line s, and the correct file name is formed in line s2 thanks to the R symbol in front of it.

The result of the program

C:    extfile.txt
l = 15
C:\1\textfile.txt
l2 = 17

 

8. Multi-line blocks of text. Example

Using the triple quotation mark “” “…” “” you can form multi-line blocks of text.

Example.

# Escape-sequences
# Multiline blocks of text
s = """First line,
second line,
third line
"""

s2 = """1
2
3
4"""

print(s)
print(s2)

Result of the program

First line,
second line,
third line
1
2
3
4

 


Related topics