Reference look for tutorials Network :
Python strings are the most popular, the most commonly used type of data. You can create them from the characters in quotes. Python single quote the same double quotes. As easy to create a string and a variable assignment. E.g -
var1 = 'Hello World!' var2 = "Python Programming"
1. Access string value
Python does not support the type of character; character is treated as a length of 1
string, it is also considered to be a sub-string. To access the sub-string sections, use square brackets plus or directly index uses an index to get a substring. E.g-
#!/usr/bin/python3 var1 = 'Hello World!' var2 = "Python Programming" print ("var1[0]: ", var1[0]) print ("var2[1:5]: ", var2[1:5]) # 切片加索引,www.zyiz.net
When the code is executed, it yields the following results -
var1, [and 0]: H of the
var2, [ the 1: of 5]: ytho
2. Update string
Can be assigned to a variable by another string to "update" an existing string. The new value may be related to its previous or entirely different string. E.g -
#!/usr/bin/python3 var1 = 'Hello World!' print ("Updated String :- ", var1[:6] + 'Python')
When the code is executed, it yields the following results -
Updated String :- Hello Python
3. escape character
The following table is a list with a backslash notation escaped or non-printable characters. Escape character strings single quotes and double quotes is resolved.
Backslash | Hexadecimal characters | Description / Description |
---|---|---|
\a |
0x07 |
Ringtone or alert |
\b |
0x08 |
backspace |
\cx |
Control-x | |
\C-x |
Control-x | |
\e |
0x1b |
Escape |
\f |
0x0c |
Feed |
\M-\C-x |
Meta-Control-x | |
\n |
0x0a |
New row |
\nnn |
Octal notation, which n in the range of 0.7 |
|
\r |
0x0d |
Carriage Return |
\s |
0x20 |
Blank |
\t |
0x09 |
Tabs |
\v |
0x0b |
Vertical tab |
\x |
characterx |
|
\xnn |
In hexadecimal notation, wherein n the 0~9 , a~f or A~F range |
4. String special operators
Suppose string variables a
stored value string ' Hello
', the variable b
stored string values ' Python
', then -
Operators | Explanation | Examples |
---|---|---|
+ |
Connection - adds the value of the operator on both sides | a + b Results HelloPython |
* |
Repeat - Create a new string, string of multiple copies of the same connection | a*2 Results HelloHello |
[] |
Slice - given the specified string value index, which is a substring of the original string. | a[1] Results e |
[:] |
Range slice - substring within a given range given | a[1:4] Results ell |
in |
Membership - if the specified character exists in a given string is returnedtrue |
'H' in a Results 1 |
not in |
Membership - if given the specified character string does not exist, then returnstrue |
'Y' not in a Results 1 |
r/R |
The original string - to suppress the actual meaning of the escape character. Identical format string syntax normal original string, the original string operators in addition to with the letter before the quotation marks " r ." " " r Can be lowercase ( r ) or capital ( R ), and should be placed immediately before the first quotation mark. |
print(r'\n') Print \n , or print(R'\n') prints \n , to note is that if you do not add r or R as a prefix, the result is a print wrap. |
% |
Format - performs string formatting | See section 5 article |
The string formatting operator
One feature is the coolest Python string format operator %
. This string is unique to the operator, to make up the C language printf()
family of functions. The following is a simple example -
#!/usr/bin/python3 print ("My name is %s and weight is %d kg!" % ('Maxsu', 71))
When the code is executed, it yields the following results -
My name is Maxsu and weight is 71 kg!
The following is the %
complete list of the symbol set used with the symbol -
Numbering | Formatting symbols | Change |
---|---|---|
1 | %c |
character |
2 | %s |
By before formatting str() function converts a String |
3 | %i |
Decimal integer with a sign |
4 | %d |
Decimal integer with a sign |
5 | %u |
无符号十进制整数 |
6 | %o |
八进制整数 |
7 | %x |
十六进制整数(小写字母) |
8 | %X |
十六进制整数(大写字母) |
9 | %e |
指数符号(小写字母’e ‘) |
10 | %E |
指数符号(大写字母’E ‘ |
11 | %f |
浮点实数 |
12 | %g |
%f 和%e |
13 | %G |
%f 和%E |
其他支持的符号和功能如下表所列 -
编号 | 符号 | 功能 |
---|---|---|
1 | * |
参数指定宽度或精度 |
2 | - |
左对齐 |
3 | + |
显示标志或符号 |
4 | <sp> |
在正数之前留空格 |
5 | # |
根据是否使用“x ”或“X ”,添加八进制前导零(‘0 ‘)或十六进制前导’0x ‘或’0X ‘。 |
6 | 0 |
使用零作为左边垫符(而不是空格) |
7 | % |
‘%% ‘留下一个文字“% ” |
8 | (var) |
映射变量(字典参数) |
9 | m.n. |
m 是最小总宽度,n 是小数点后显示的位数(如果应用) |
6.三重引号
Python中的三重引号允许字符串跨越多行,包括逐字记录的新一行,TAB
和任何其他特殊字符。
三重引号的语法由三个连续的单引号或双引号组成。
#!/usr/bin/python3 para_str = """this is a long string that is made up of several lines and non-printable characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ \n ], or just a NEWLINE within the variable assignment will also show up. """ print (para_str)
当执行上述代码时,会产生以下结果。注意每个单独的特殊字符如何被转换成其打印形式,它是直到最后一个NEWLINEs
在“up
”之间的字符串的末尾,并关闭三重引号。 另请注意,NEWLINEs
可能会在一行或其转义码(\n
)的末尾显式显示回车符 -
this is a long string that is made up of several lines and non-printable characters such as TAB ( ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ ], or just a NEWLINE within the variable assignment will also show up.
原始字符串根本不将反斜杠视为特殊字符。放入原始字符串的每个字符都保持所写的方式 -
#!/usr/bin/python3 print ('C:\\nowhere')
当执行上述代码时,会产生以下结果 -
C:\nowhere
现在演示如何使用原始的字符串。将表达式修改为如下 -
#!/usr/bin/python3 print (r'C:\\nowhere')
当执行上述代码时,会产生以下结果 -
C:\\nowhere
7.Unicode字符串
在Python 3中,所有的字符串都用Unicode表示。在Python 2内部存储为8
位ASCII,因此需要附加’u
‘使其成为Unicode,而现在不再需要了。