(1) string
Strings are the most commonly used data type in Python. We can use quotation marks ( ' or " ) to create strings. As the name suggests, kebabs are strings made of mutton, and strings are made up of characters. The essence of strings is: a sequence of characters .
var1 = 'Hello World!'
var2 = "Python"
(2) Encoding of strings
![](https://img-blog.csdnimg.cn/be0b3a19e07043ea84a05e32bf62a63b.png)
Conversion Tips:
Use the built-in function ord() to convert characters into corresponding Unicode codes;Use the built-in function chr() to convert decimal numbers into corresponding characters;
print(ord('A')) #65
print(ord('成')) #25104
print(chr(66)) #'B'
print(ord('龙') )#40857
(3) Quotation marks create a string
a = "I'm a teacher!"
print(a) #I'm a teacher!
b = 'my_name is "Jackie Chan"'
print(b) #my_name is "Jackie Chan"
![](https://img-blog.csdnimg.cn/c7f06f2e84d3495894ce8f3107b3df92.png)
(4) Empty string and len() function
c = ''
print(len(c)) #结果:0
(5) Escape characters
escape character | describe |
---|---|
\ (at the end of the line) | continuation character |
\\ | backslash symbol |
\' | apostrophe |
\" | Double quotes |
\a | ring the bell |
\b | Backspace |
\e | escape |
\000 | null |
\n | new line |
\v | vertical tab |
\t | horizontal tab |
\r | carriage return |
\f | change page |
\oyy | Octal number, y stands for characters from 0 to 7, for example: \012 stands for newline. |
\xyy | Hexadecimal number, starting with \x, the character represented by yy, for example: \x0a represents newline |
\other | Other characters are output in normal format |
the code
a = 'I\nlove\bU'
print(a)
print('a\rabb\\cc')
(6) Python string operators
The value of instance variable a in the following table is the string "Hello", and the value of variable b is "Python":
operator | describe | example |
---|---|---|
+ | string concatenation | >>>a + b 'HelloPython' |
* | Repeat output string | >>>a * 2 'HelloHello' |
[] | Get characters in a string by index | >>>a[1] 'e' |
[ : ] | Extract part of a string | >>>a[1:4] 'ell' |
in | Membership operator - returns True if the given character is contained in the string | >>>"H" in a True |
not in | Membership operator - returns True if the given character is not contained in the string | >>>"M" not in a True |
r/R | Raw Strings - Raw Strings: All strings are used literally, without escaping special or unprintable characters. Raw strings have almost the same syntax as normal strings, except that the letter "r" (upper and lower case) is added before the first quotation mark in the string. | >>>print r'\n' \n >>> print R'\n' \n |
% | format string | see next chapter |
specific code
a = "Hello"
b = "Python"
print("a + b 输出结果:", a + b)
print("a * 2 输出结果:", a * 2)
print("a[1] 输出结果:", a[1])
print("a[1:4] 输出结果:", a[1:4])
if ("H" in a):
print( "H 在变量 a 中")
else:
print( "H 不在变量 a 中")
if ("M" not in a):
print("M 不在变量 a 中")
else:
print("M 在变量 a 中")
print(r'\n')
print(R'\n')
running result:
-
If both sides of the + are strings, concatenate.
-
Addition if both sides of + are numbers
-
If the types of the two sides of the + are different, an exception is thrown
You can concatenate multiple literal strings directly together. Example : 'aa' 'bb' results in 'aabb'
a = 'sxt'+'qiqi' #结果是:'sxtqiqi'
b = 'sxt''qiqi' #结果是:'sxtqiqi'
(7) String slice slice operation
[start offset start: end offset end: step size step]
Operation and instructions | example | result |
[:] extracts the entire string | “abcdef”[:] | "abcdef" |
[start:] from the start index to the end | “abcdef”[2:] | “cdef” |
[:end] Know end-1 from scratch | “abcdef”[:2] | “ab” |
[start:end] from start to end-1 | “abcdef”[2:4] | “cd” |
[start:end:step] Extract from start to end-1 , the step size is step | “abcdef”[1:5:2] | “bd” |
example | illustrate | result |
"abcdefghijklmnopqrstuvwxyz"
[-3:]
|
last three | “xyz” |
"abcdefghijklmnopqrstuvwxyz"
[-8:-3]
|
The eighth from the last to the third from the last (
include header but not tail
)
|
'stuvw' |
"abcdefghijklmnopqrstuvwxyz"
[::-1]
|
Negative step size, reverse extraction from right to left
|
'zyxwvutsrqponmlkjihgfedcba' |
注意:切片操作时,起始偏移量和终止偏移量不在[0,字符串长度-1]这个范围,也不会报错。起始偏移量小于0则会当做0,终止偏移量大于“长度-1”会被当成-1。例如:
(八)split()分割和join()合并
a = "to be or not to be"
print(a.split())
print(a.split('be'))
![](https://img-blog.csdnimg.cn/0129752e48c041759f30872c967b5b0e.png)
a = ['sxt','sxt100','sxt200']
print('*'.join(a))
运行效果:
总结:
拼接字符串要点:使用字符串拼接符 + ,会生成新的字符串对象,因此不推荐使 用 + 来拼接字符串。推荐使用 join 函数,因为 join 函数在拼接字 符串之前会计算所有字符串的长度,然后逐一拷贝,仅新建一次对象。
(九) 字符串常用方法汇总
![](https://img-blog.csdnimg.cn/f9c005c0ad6f4d52ab7c0ddee4b4e7bb.png)
a='''ablchbjj '''
方法和使用示例 | 说明 | 结果 |
len(a) | 字符串长度 | 8 |
a.startswith('abl') | 以指定字符串开头 | True |
a.endswith('jj') | 以指定字符串结尾 | True |
a.find('b') | 第一次出现指定字符串的位置 | 1 |
a.rfind('b') | 最后一次出现指定字符串的位置 | 5 |
a.count("lch") | 指定字符串出现了几次 | 1 |
a.isalnum() | 所有字符全是字母或数字 | True |