Python3字符串学习教程
|
字符串类型是python里面最常见的类型,是不可变类型,支持单引号、双引号、三引号,三引号是一对连续的单引号或者双引号,允许一个字符串跨多行。 下面列举一些与字符串类型有关的模块。 Python 3 的源码的默认编码方式为 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''' 'beginnandnnextnend' 字符串是不可修改的,这点很重要!你可以把它想象为字符组成的元组。 >>> 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','o','W','r','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>",in <module> s1s2 NameError: name 's1s2' is not defined >>> s1+s2 'HelloWorld' 字符串操作和方法: >>> 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 = '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','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" #少了一个感叹号哦!
再次注意:以上方法都没有改变原字符串,字符串是不可改变的! 以下简单看看: 让我们重点关注一个强大的格式化方法 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],它们叫做复合字段名,你可以:
>>> 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')
>>> 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
在替换域中,你还可以使用格式说明符。冒号 : 标示格式说明符的开始。
>>> pi = 3.141592653
>>> print("The pi is {0:10.3f}".format(pi)) # 0:10.3f 表示输出宽度为10,保留三位小数,浮点数
The pi is 3.142
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
