字符串str

capitalize

将字符串首字母转换成大写,其余全部小写

#!/usr/bin/python
#-*-codeing: UTF-8-*-
test = 'color'
v = test.capitalize()
print(v)
# Color
casefold

将字母转换成小写

#!/usr/bin/python
#-*-codeing: UTF-8-*-
test = 'COLOR'
v = test.casefold()
print(v)
# color
center

将字符串在指定的长度中居中,默认用空格占位,可用第二个参数指定占位符

#!/usr/bin/python
#-*-codeing: UTF-8-*-
test = 'COLOR'
v = test.center(20, '*')
print(v)
# *******COLOR********
ljust

字符串左边填充

#!/usr/bin/python
#-*-codeing: UTF-8-*-
s = 'alex'
v = s.ljust(20, '*')
print(v)
# alex****************
rjust

字符串右边填充

#!/usr/bin/python
#-*-codeing: UTF-8-*-
s = 'alex'
v = s.rjust(20, '*')
print(v)
# ****************alex
S.count(sub[, start[, end]]) -> int

字符在字符串中出现的次数

#!/usr/bin/python
#-*-codeing: UTF-8-*-
test = 'COLOR'
v = test.count('C')
v1 = test.count('C', 1)
print(v, v1)
# 1 0
v2 = test.count('A')
print(v2)
# 0
S.endswith(suffix[, start[, end]]) -> bool

字符串是否已某个字符结尾

#!/usr/bin/python
#-*-codeing: UTF-8-*-
test = 'COLOR'
v1 = test.endswith('C')
print(v1)
# False
v2 = test.endswith('R')
print(v2)
# True
v3 = test.endswith('O', 3, 4)
print(v3)
# True
v4 = test.endswith('R', 4)
print(v4)
# True
S.find(sub[, start[, end]]) -> int

查找子项在字符串的位置,返回第一次匹配到的索引,查询不到返回-1

#!/usr/bin/python
#-*-codeing: UTF-8-*-
s = 'alex'
v1 = s.find('ex')
print(v1)
# 2
v2 = s.find('vw')
print(v2)
# -1
v3 = s.find('al', 2)
print(v3)
# -1
S.format(*args, **kwargs) -> str
S.format_map(mapping) -> str
#!/usr/bin/python
#-*-codeing: UTF-8-*-
s = 'I like {sport} and {lesson}'
v1 = s.format(sport = 'basketball', lesson = 'movie')
v2 = s.format_map({'sport': 'basketball', 'lesson': 'movie'})
print(v1)
print(v2)
# I like basketball and movie
# I like basketball and movie
v = input('>>>')
name, address, hobbit = v.split(',')
s = '敬爱可亲的{name}, 最喜欢在{address}地方干{hobbit}'
v1 = s.format(**{'name': name, 'address': address, 'hobbit': hobbit})
print(v1)
S.index(sub[, start[, end]]) -> int

同find,但是查询不到子字符串会报错,建议使用find。

#!/usr/bin/python
#-*-codeing: UTF-8-*-
s = 'alex'
v = s.index('e')
print(v)
# 2
isalnum

字符串必须是由数字、字母组成,并且至少包含一个字符。

#!/usr/bin/python
#-*-codeing: UTF-8-*-
s = 'alex98k'
v = s.isalnum()
print(v)
# True
s1 = ''
v1 = s1.isalnum()
print(v1)
# False
s2 = 'alex@'
v2 = s2.isalnum()
print(v2)
# False
expandtabs
#!/usr/bin/python
#-*-codeing: UTF-8-*-
s = 'name\tage\tsex\nliming\t18\tmen\nwutong\t23\twomen\nshihao\t16\tmen'
v = s.expandtabs(20)
print(v)
# name                age                 sex
# liming              18                  men
# wutong              23                  women
# shihao              16                  men
isdecimal、isdigit、isnumeric

isdecimal识别十进制数字
isdigit识别②
isnumeric识别中文大写数字

#!/usr/bin/python
#-*-codeing: UTF-8-*-
s = '1'
v1 = s.isdecimal()
v2 = s.isdigit()
v3 = s.isnumeric()
print(v1, v2, v3)
#T rue True True
s2 = '②'
v4 = s2.isdecimal()
v5 = s2.isdigit()
v6 = s2.isnumeric()
print(v4, v5, v6)
# False True True
s3 = '二'
v7 = s3.isdecimal()
v8 = s3.isdigit()
v9 = s3.isnumeric()
print(v7, v8, v9)
# False False True
istitle、title

判断是否是标题
将字符串转换成标题

#!/usr/bin/python
#-*-codeing: UTF-8-*-
s = 'Return True if the string is a decimal string, False otherwise.'
v1 = s.istitle()
print(v1)
# False
v2 = s.title()
print(v2)
# Return True If The String Is A Decimal String, False Otherwise.
v3 = v2.istitle()
print(v3)
# True
join

Example: ‘.’.join([‘ab’, ‘pq’, ‘rs’]) -> ‘ab.pq.rs

#!/usr/bin/python
#-*-codeing: UTF-8-*-
s = '你是我的小呀小苹果'
print(s)
# 你是我的小呀削苹果
t = ' '
v = t.join(s)
print(v)
# 你 是 我 的 小 呀 削 苹 果
lower、islower、upper、isupper

小写转换与校验,大写转换与校验

test = 'Alex'
v1 = test.islower()
print(v1)
# False
v2 = test.lower()
print(v2)
# alex
v3 = v2.islower()
print(v3)
# True
test = 'alex'
v1 = test.isupper()
print(v1)
# False
v2 = test.upper()
print(v2)
# ALEX
v3 = v2.isupper()
print(v3)
# True
strip、lstrip、rstrip

去掉字符串两边、左边、右边的空格或指定的字符串。当指定匹配字符串时,按照最多匹配原则依次多次匹配。
可以去换行符\n和制表符\t

test = 'xalex'
v1 = test.strip('x')
print(v1)
# ale
v2 = test.lstrip('x')
print(v2)
# alex
v3 = test.rstrip('x')
print(v3)
# xale
v4 = test.rstrip('exslxa')
# 先删除ex
# xal
# 再删除l
# xa
# 再删除xa
print(v4)
#
maketrans、translate

用相应的对应关系替换字符串内容

test = "1234,其实14你懂的!1234,喜欢你"
m = str.maketrans('1234', 'alex')
new_test = test.translate(m)
print(new_test)
# alex,其实ax你懂的!alex,喜欢你
partition、rpartition、split、rsplit

partition、rpartition包含分隔符;
split、rsplit不包含分隔符。
split、rsplit接收第二个参数表示分割几次。

test = '一班;二班;三班'
# 只分割一次
v1 = test.partition(';')
print(v1)
# ('一班', ';', '二班;三班')
# 从右边开始分割一次
v2 = test.rpartition(';')
print(v2)
# ('一班;二班', ';', '三班')
# 全分割
v3 = test.split(';')
print(v3)
# ['一班', '二班', '三班']
# 分割指定次数
v4 = test.rsplit(';', 1)
print(v4)
# ['一班;二班', '三班']
splitlines

根据换行符\n进行分割

# 根据换行符\n进行分割
test = '中国\n美国\n日本\n韩国\n'
# False不包含换行符
v1 = test.splitlines(False)
print(v1)
# ['中国', '美国', '日本', '韩国']
# True包含换行符
v2 = test.splitlines(True)
print(v2)
# ['中国\n', '美国\n', '日本\n', '韩国\n']
swapcase

将大写字母转换成小写,小写字符串转换成大写

# 将大写字母转换成小写,小写字符串转换成大写
test = 'abcd'
v1 = test.swapcase()
print(v1)
# ABCD
test2 = 'XYZ'
v2 = test2.swapcase()
print(v2)
# xyz
test3 = 'abcdXYZ'
v3 = test3.swapcase()
print(v3)
# ABCDxyz
replace
test = 'alexhttpexsex'
# 全部替换
v = test.replace('ex', '1234')
print(v)
# al1234http1234s1234
# 指定替换次数
v1 = test.replace('ex', '1234', 2)
print(v1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值