Python 3语法小记(五)字符串

时间:2023-03-09 00:25:01
Python 3语法小记(五)字符串

Python 3 的源码的默认编码方式为 UTF-8

在Python 3,所有的字符串都是使用Unicode编码的字符序列。

utf-8 是一种将字符编码成字节序列的方式。字节即字节,并非字符。字符在计算机内只是一种抽象。字符串则是一种抽象的序列。在这里我们只讨论字符串,不讨论字节。

在Python 中,字符串可以用单引号括起来,也可以用双引号,甚至是三引号。

但如果字符串中含有单引号,你应该用双引号来括,或者用转义符加单引号括起来。含有双引号的同理。

三引号的字符串可以换行!

>>> 'Let's go!'
SyntaxError: invalid syntax
>>> "Let's go!"
"Let's go!"
>>> 'Let\'s go!'
"Let's go!"
>>> '''begin
and
next
end'''
'begin\nand\nnext\nend'

字符串是不可修改的,这点很重要!你可以把它想象为字符组成的元组。

>>> s = "HelloWorld"
>>> s[0] = 'h'
Traceback (most recent call last):
File "<pyshell#123>", line 1, in <module>
s[0] = 'h'
TypeError: 'str' object does not support item assignment

如果你想修改它,可以先转换成列表,修改完成后再转为字符串。

>>> s
'HelloWorld'
>>> L = list(s)
>>> L
['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
>>> L[0] = 'h'
>>> ''.join(L) #这是什么?别着急,下面我们会谈到
'helloWorld'

字符串可以进行直接拼接,但如果是两个变量代表的字符串,你还是用 + 号吧

>>> s = "Hello""World"
>>> s
'HelloWorld'
>>> s1 = "Hello"
>>> s2 = "World"
>>> s1s2
Traceback (most recent call last):
File "<pyshell#138>", line 1, in <module>
s1s2
NameError: name 's1s2' is not defined
>>> s1+s2
'HelloWorld'

字符串操作和方法:

len(s)  返回字符串长度

x in s 查询 x 是否在 s 中

>>> s = "HelloWorld"
>>> len(s)
10
>>> "ll" in s
True

s.find( x )  在字符串 s 中找子串 x ,找到则返回最左端的索引,找不到则返回-1

>>> s
'HelloWorld'
>>> s.find("l")
2
>>> s.find('w')
-1

s.splitlines() 将多行字符串分割成多个单行字符串组成的列表,换行符被吸收

>>> s = '''begin
...then..
...next..
end...'''
>>> s.splitlines()
['begin', '...then..', '...next..', 'end...']

s.split( x ) 以 x 作为分隔符将 s 分割成一个字符串列表,如果不提供x,则程序会自动将所有空格和换行作为分隔符分割

>>> s = "here#there"
>>> s.split('#') # #作为分隔符
['here', 'there']
>>> s = '''begin
.then.. and
.next.
end'''
>>> s.split() #默认情况将所有换行和空格都分割
['begin', '.then..', 'and', '.next.', 'end']

s.lower()   返回s 的小写形式的字符串

s.upper() 返回 s 的大写形式的字符串

>>> s = 'HelloWorld'
>>> s.lower()
'helloworld'
>>> s.upper()
'HELLOWORLD'

s.join( seq )  split 的逆方法,将序列 seq 用 s 连接起来,必须是字符串序列

>>> L = ['1','33','42']
>>> '+'.join(L) #用+来连接
'1+33+42'
>>> L = list(s) #拆开s
>>> L
['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
>>> ''.join(L) #粘合了。。。
'HelloWorld'

s.replace( x, y)  将 s 中所有匹配 x 的项用 y 代替,如果找不到,那就直接返回 s 咯

>>> s
'HelloWorld'
>>> s.replace("World","Cheng")
'HelloCheng'
>>> s.replace("world","Cheng") #知道为什么吧...
'HelloWorld'

s.strip( x ) 如果不提供 x,那么返回去除了首尾两侧空格的字符串,如果提供了字符串 x,那么将去除s首尾所有在 x 中的字符并返回

>>> s = "   Hi,I'm Alice!   "
>>> s.strip()
"Hi,I'm Alice!"
>>> s.strip("! ") #这是两个字符哦
"Hi,I'm Alice" #少了一个感叹号哦!

再次注意:以上方法都没有改变原字符串,字符串是不可改变的!

以下简单看看:

s.starstwith( x )  测试 s 是否以 x 开头

s.endswith( x )  测试 s 是否以 x 结尾

s.isalnum()  测试 s 是否全是字母和数字,并至少有一个字符

s.isalpha()   测试 s 是否全是字母,并至少有一个字符

s.isdigit()  测试 s 是否全是数字,并至少有一个字符

s.isspace()  测试 s 是否全是空白字符,并至少有一个字符

s.islower() 测试 s 中的字母是否全是小写

s.isupper() 测试 s 中的字母是否便是大写

s.istitle() 测试 s 是否是首字母大写的

让我们重点关注一个强大的格式化方法 format ,看下面代码

>>> name = 'Jonh'
>>> call = '13560300xxx'
>>> "{0}'s telephone number is {1}".format(name,call) # (1)
"Jonh's telephone number is 13560300xxx"
>>> addr = "A103"
>>> "{0}'s telephone number is {1} and his address is {2}".format(name,call,addr) #(2)
"Jonh's telephone number is 13560300xxx and his address is A103"

(1)句中,字符串中 {0} 被 format 的第一个参数代替,{1} 被第二个参数代替。两个参数不够?事实上,你可以给予它任意多个参数,然后用相同个数的替换字段进行替换。什么是替换字段?{0},{1}就叫做
替换字段。我们在第(2)句中使用了3个替换字段,{0}对应name, {1}对应call,{2}对应addr。再多的参数也类似于刚才的做法。

那么,仅仅是这样?当然不是!让我们继续看

>>> L = [2,3,5,7]
>>> print("x is {0[0]}, y is {0[2]}".format(L))
x is 2, y is 5

{0[0]} 表示L[0],{0[2]} 表示L[2],它们叫做 复合字段名,你可以:

(1)使用列表作为参数,并且通过下标索引来访问其元素(跟上一例类似)

(2)使用字典作为参数,并且通过键来访问其值

>>> d
{'b': 2, 'a': 1}
>>> print("x is {0[a]}, y is {0[b]}".format(d))
x is 1, y is 2
>>> d = {2:3.5,7:4.5}
>>> print("x is {0[2]}, y is {0[7]}".format(d))
x is 3.5, y is 4.5

d 为字典,a 、b为键,{0[a]} 对应到了值2(注意:是a,b,不是'a', 'b')

(3)使用模块作为参数,并且通过名字来访问其变量及函数

>>> print("{0.random}".format(random))
<built-in method random of Random object at 0x022D61F8>

(4)使用类的实例作为参数,并且通过名字来访问其方法和属性

>>> class A:
pass >>> x = A()
>>> print("The class is {0.__class__}".format(x))
The class is <class '__main__.A'>

(5)以上方法的任意组合

替换字段除了整数,你还可以使用参数名字

>>> print("{name}'s telephone number is {call}".format(name = "Jonh",call = 69993))
Jonh's telephone number is 69993

在替换域中,你还可以使用
格式说明符。冒号 : 标示格式说明符的开始。

格式说明符请见:http://blog.****.net/jcjc918/article/details/9354815

>>> pi = 3.141592653
>>> print("The pi is {0:10.3f}".format(pi)) # 0:10.3f 表示输出宽度为10,保留三位小数,浮点数
The pi is 3.142