Python escape character

Python uses a backslash (\) escape character when you need to use special characters in characters. As shown below:

escape character

description

\(at the end of the line)

Continuous character

\\

backslash symbol

\'

single quotes

\"

double quotes

\a

Bell

\b

Backspace

\e

escape

\000

empty

\n

Line break

\v

Vertical Tabs

\t

Horizontal tabs

\r

Enter

\f

Change page

\oyy

octal number, the character represented by yy, for example: \o12 stands for line break

\xyy

hexadecimal number, the character represented by yy, for example: \x0a stands for line break

\other

Other characters are output in normal format

Python string operator

The following table instance variable a has the value string "Hello" and the b variable value is "Python":

Operator

Description

Instance

+

String connection

a + b Output: HelloPython

*

Repeat output string

a*2 Output: HelloHello

[]

Get the characters in the string by index

a[1] Output e

[ : ]

Take a section of the string and follow the Left Closed Right principle, str[0,2] does not contain the third character.

a[1:4] Output ell

in

Member operator - Returns True if the string contains the given character

'H' in a Outputs True Td>

not in

member operator - returns True if the string does not contain the given character

'M' not in a output True

r/R

Original String - Original String: All strings are used literally, without special characters that can be escaped or not printed.
The original string has almost exactly the same syntax as a normal string, except that the letter r (which can be capitalized) is preceded by the first quotation mark in the string.

a + b Output result：HelloPython
a *2Output result：HelloHello
a[1]Output result： e
a[1:4]Output result： ell
H In the variable a in
M Not in variable a in
\n
\n

Python string formatting

Python supports the output of formatted strings. Although this may use very complex expressions, the most basic use is to insert a value into a string with the string formatter %s.

In Python, string formatting uses the same syntax as the sprintf function in C.

Instance (Python 3.0+)

#!/usr/bin/python3print("My name is %s %d this year!" % ('小明', 10))

The output of the above example:

My name isKomyothis year10year old!

python string formatting symbol:

symbol number

Description

&nbsp ; %c

Formatting characters and their ASCII code

%s< /font>

Format String

%d

Format integers

< Font style="line-height: 23px; " size="2" face="宋体"> %u

Format unsigned integer

&nbsp ; %o

Format unsigned octal number< /td>

%x Font>

Format unsigned hexadecimal number

%X

Format unsigned hexadecimal (uppercase) Td>

%f

Format floating point numbers to specify the precision after the decimal point< /td>

%e

Format floating point numbers using scientific notation

%E

acts like %e, formatting floating point numbers using scientific notation

%g

%f and %e shorthand

%G

%f and %E shorthand

%p

Format the address of the variable with a hexadecimal number

Format operator helper instructions:

symbol

function

*

Define width or decimal precision

-

Use left alignment

+

Show the plus sign ( + )

in front of the positive number

<sp>

Show spaces before positive numbers

#

displays zero ('0') in front of the octal number and '0x' or '0X' in front of the hexadecimal (depending on whether it is 'x' or 'X')

0

The displayed number is padded with '0' instead of the default space

%

'%%' outputs a single '%'

(var)

Map Variables (Dictionary Parameters)

m.n.

m is the minimum total width of the display, and n is the number of digits after the decimal point (if available)

Beginning with Python 2.6, a new function for formatting strings has been added.

*

Define width or decimal precision

-

Use left alignment

+

Show the plus sign ( + )

in front of the positive number

<sp>

Show spaces before positive numbers

#

displays zero ('0') in front of the octal number and '0x' or '0X' in front of the hexadecimal (depending on whether it is 'x' or 'X')

0

The displayed number is padded with '0' instead of the default space

%

'%%' outputs a single '%'

(var)

Map Variables (Dictionary Parameters)

m.n.

m is the minimum total width of the display, and n is the number of digits after the decimal point (if available)

Beginning with Python 2.6, a new function for formatting strings has been added

The above example execution result is:

This is an instance of a multi-line stringMultiple-line strings can use tabs
TAB (). You can also use line breaks[].

Three quotes free the programmer from the quagmire of quotes and special strings, keeping a small string of formatting from start to finish in the so-called WYSIWYG (what you see is what you get) format.

A typical use case is that when you need a piece of HTML or SQL, then string combination, special string escaping will be very cumbersome.

Unicode string

In Python 2, normal strings are stored in 8-bit ASCII, while Unicode strings are stored as 16-bit unicode strings, which can represent more character sets. The syntax used is to prefix the string with u.