Python 基础知识(5): 基本数据类型之字符串(Ⅱ)
字符串方法
17.join: 对字符串进行拼接
- x="can"
- y="li"
- y.join(x)
结果:
'clialin'
18.ljust,rjust 使字符串左 (右对齐), 并用某个字符对右(左端) 进行填充
- sen="God"
- print(sen.ljust(10,"#"))
- print(sen.rjust(10,"#"))
结果:
- God#######
- #######God
19.zfill: 在字符串左端填充 "0"
- sen="God"
- print(sen.zfill(10))
结果:
0000000God
20.center: 返回一个居中的字符串
- sen="God"
- print(sen.center(10,"+"))
结果:
+++God++++
21.lstrip,rstrip: 从左 (右) 端开始去除字符串中的某一个字符
- sen="moment"
- print(sen.lstrip("m"))
- print(sen.rstrip("nt"))
结果:
oment
mome
22.partition,rpatition: 根据某个字符把字符串分割成三部分
- sen="banana"
- print(sen.partition("n"))
- print(sen.rpartition("n"))
结果:
- ('ba', 'n', 'ana')
- ('bana', 'n', 'a')
23.split,rsplit: 利用某个字符对字符串进行切片, 可指明次数
- sen="banana"
- print(sen.split("a",1))
- print(sen.rsplit("a"))
结果:
- ['b', 'nana']
- ['b', 'n', 'n', '']
24.swapcase: 把字符串中的大写字符转成小写, 小写字母转成大写
- sen="Hello,World."
- print(sen.swapcase())
结果:
hELLO,wORLD.
python 学习之旅(六)
来源: http://www.bubuko.com/infodetail-2849956.html