Python string操作

Python 内置函数收集

1.python3支持Unicode
使用ord()可以把字符转化成对应的Unicode码
使用chr()可以把数字转化为字符串

2.字符替换操作

a = ‘asdfasdfadf’
a
‘asdfasdfadf’

a.replace(‘a’,‘f’)
‘fsdffsdffdf’

a
‘asdfasdfadf’

a = a.replace(‘a’,‘f’)
a
‘fsdffsdffdf’

3.字符串切片操作

a = “abcdefghijklmn”
a
‘abcdefghijklmn’

a[::]
‘abcdefghijklmn’

a[1:2:3]
‘b’

a[0:7:3]
‘adg’

a[::-1]
‘nmlkjihgfedcba’

4.字符串分割split()合并join()

a = “to b or not to b”
a.split()
[‘to’, ‘b’, ‘or’, ‘not’, ‘to’, ‘b’]

a.split(“b”)
['to ', ’ or not to ', ‘’]

a = [‘to’,‘be or not’,‘to be’]
a
[‘to’, ‘be or not’, ‘to be’]

"".join(a)
'to
be or not*to be’

“”.join(a)
‘tobe or notto be’
join的运行时间远小于+=

5.字符串驻留机制

只有符合标识符规则的字符串(仅包含下划线、字母和数字)会启用驻留机制

b = “3#”
c = “3#”
b is c
False

b = “3_”
c = “3_”
b is c
True

6.成员操作符 in、no int
判断字符是否包含在字符串中

7.常用查找

a = “abcdefghijklmn”
len(a)
14

a.startswith(“c”)
False

a.startswith(“a”)
True

a.endswith(“n”)
True

a.find(“e”)
4

a.rfind(“e”)
4

a.count(“c”)
1

a.isalnum()
True

8.去除首尾信息

a = “ryt
a.strip(’’)
'r
y*t’

a.lstrip(’’)
'r
yt

a.rstrip(’’)
'ry
t’

9.大小写转换

a = “Ren you Tong”
a.capitalize()
‘Ren you tong’

a.title()
‘Ren You Tong’

a.upper()
‘REN YOU TONG’

a.lower()
‘ren you tong’

a.swapcase()
‘rEN YOU tONG’

10.格式排版
center() ljust() rjust()

11.else
isalnum
isalpha
isalspace
isupper
islower

12.format()

a = “name is {name}, age is {age}”
b = a.format(name = “ren youtong”, age = 24)
b
‘name is ren youtong, age is 24’

“name is {0}, age is {1:*^8}”.format(“Renyoutong”,24)
‘name is Renyoutong, age is 24

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值