1. use three quotation marks ( "" or "" ")
Using the three quotes, you can indicate a multiline string. You can use single and double quotation marks freely in three quotes. For example:
str= "This is a multi-line string. the first line.
The second line.
"What ' s your name?," I asked.
He said "Bond, James bond."
" "
2. Natural string
If you want to indicate certain strings that do not require special handling such as escape characters (invalid escape symbols), then you need to specify a natural string. Natural strings are specified by prefixing r
or specifying the string R
. such as str= r"Newlines are indicated by \n"
.
The print (str) result is:newlines is indicated by \ n.
Be sure to use a natural string to process the regular expression. Otherwise, you will need to use a lot of back slashes. For example, a forward reference can be written as ' \\1 ' or R ' \1 '.
3. The string is immutable
This means that once you create a string, you can no longer change it.
4. string literals by literal meaning
If you put two strings next to each other by literal means , they will be automatically connected by Python. For example, ' what\ ' s ' your Name? '
" What ' s your name? "
5. Object
6. Variable
7. Logical lines and physical lines
If you want to use more than one logical line in a physical line, you need to use a semicolon (;) to specifically indicate this usage. semicolon represents the end of a logical line/statement. For example: I=5;print (i)
Example of writing a logical line in multiple physical rows. It is called the Explicit row connections
s =
" is a string. \
This continues the string. '
print (
s)
输出结果为:This is a string. This continues the string.
在逻辑行中使用了圆括号、方括号或波形括号的时候,也可以使多个物理行对应一个逻辑行,这被称为暗示的行连接。
str=[1,23,"hello"
,"ok"]
print(str)
结果为:[1, 23, ‘hello‘, ‘ok‘]
Python Concise Tutorial Learning Notes