您好,欢迎访问一九零五行业门户网

Python3字符串学习教程

字符串类型是python里面最常见的类型,是不可变类型,支持单引号、双引号、三引号,三引号是一对连续的单引号或者双引号,允许一个字符串跨多行。
字符串连接:前面提到的+操作符可用于字符串连接,还可以直接把几个字符串连在一起写,另外调用join()方法也可以连接字符串。
只适用于字符串连接的操作符:前面提到了一些序列类型共用的操作符,除此之外,字符串还有只属于自己的操作符,包括格式控制操作符%、字符串模板string.template、原始字符串操作符r/r、unicode字符串操作符u/u。
下面列举一些与字符串类型有关的模块。
string:字符串操作相关函数和工具,比如template类。
re:正则表达式,强大的字符串模式匹配模块。
struct:字符串和二进制之间的切换。
c/stringio:字符串缓冲对象,操作方法类似于file对象。
base64:base16,32,64数据编解码。
codecs:解码器注册和基类。
crypt:进行单方面加密。
difflib:找出序列间的不同。
hashlib:多种不同安全哈系算法和信息摘要算法的api,python2.5废除。
hma:hmac信息鉴权算法的python实现。
md5:rsa的md5信息摘要鉴权。
rotor:提供多平台的加解密服务。
sha:niat的安全哈系算法sha。
stringprep:提供用于ip协议的unicode字符串。
textwrap:文本打包和填充。
unicodedata:unicode数据库。
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 , line 1, in 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 = helloworld >>> s 'helloworld' >>> s1 = hello >>> s2 = world >>> s1s2 traceback (most recent call last): file , line 1, in 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))
(4)使用类的实例作为参数,并且通过名字来访问其方法和属性
>>> class a: pass >>> x = a() >>> print(the class is {0.__class__}.format(x)) the class is
(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
其它类似信息

推荐信息