String escaping
Escapes"\"
Meaning escape string that some characters have special meaning identified as an ordinary character, see the escape character in the string parsing function, it is no longer doing its special treatment, but as a normal character printing.
When the backslash "\" when the end of a line, meaning that line breaks. (Generally only used in the case of long codes)
str = 'hello,' \ 'world' print(str) >>> hello,world
If the backslash "\" is not a position where the end of the line, and followed by a special character, it means "\" special characters behind no special treatment.
print("Let\'s go!") >>>Let's go! print('\"Hello world!\" she said') >>>"Hello world!" she said print("\"Hello world!\" she said") >>>"Hello world!" she said
If you want to wrap may be used in the intermediate code \ n
str = 'hello,\nworld' print(str) >>> hello, >>> world
# Escaped there are a lot of writing for different functions temporarily slightly. . .
String formatting
% Formatted using : a plurality of variables to be inserted, you must use the tuple.
info = "my name is %s . I'm %s ." % ('xxx', 18) print(info) >>> my name is xxx . I'm 18 .
If the strings are different and the number of parameters to be formatted, an exception is thrown.
If too many parameters, code readability becomes low. And python official document does not recommend using the% format string.
Use str.format () format string
str.format () is %格式化
improved, it uses the ordinary function call syntax, and can __format__()
be extended to object methods.
When using str.fromat (), field replacement is labeled with braces.
info = "my name is {}. I'm {}.I'm from{}." .format('xxx', 18, ['china']) print(info) >>> my name is xxx. I'm 18.I'm from['china'].
Or may be referenced by an index variable in either order
info = "I'm from{2}. My name is {0}. I'm {1}." .format('xxx', 18, ['china']) print(info) >>> I'm from['china']. My name is xxx. I'm 18.
You can also specify variables
info = "I'm from{country}. My name is {name}. I'm {age}." .format(name='xxx', age=18, country=['china']) print(info) >>>I'm from['china']. My name is xxx. I'm 18.
Reading data from the dictionary also be used when **
info = {'name': 'xxx', 'age': 18} str = "my name is {name}. i'm {age}" print(str.format(**info)) >>>my name is xxx. i'm 18
When multiple parameters and longer string handling, readability is still very poor.
f-strings
It refers to the f-strings f
or F
the beginning of the string, which {}
expression contains a value will be replaced.
name = 'xxx' age = 18 print(f"my name is {name}, i'm {age}") >>> my name is xxx, i'm 18
Multi-line f-strings
name = 'xxx' age = 18 country = 'china' info = { f"my name is {name}." f"i'm {age}." f"i'm from {country}." } print(info) >>>{"my name is xxx.i'm 18.i'm from china."}
Each line must add f
a prefix, or formatting will not work. If the string contains brackets {}
, then you need to wrap it in double parentheses.
Can be escaped with a backslash character, but you can not use the f-string expression, # can not appear in an expression.
print(f"You are very \"handsome\"") >>>You are very "handsome" print(f"{You are very \"handsome\"}") >>>SyntaxError: f-string expression part cannot include a backslash
String concatenation
plus
a = 'hello' b = 'world' c = a + b print(c) >>> helloworld
Comma, pit
a = 'hello' b = 'world' c = a, b print(a, b) >>> hello world print(c) >>> ('hello', 'world')
c is a tuple
There are directly connected to the join method, format, and the string