Python笔记 之 str模块

01 将指定对象转为字符串
str(object=’’) -> str
st01 = 1000  
st = str(st01) 
02 创建字符串
str(bytes_or_buffer[, encoding[, errors]]) -> str
st02 = 'chengdu'  
st = str(b'abc',encoding='utf-8') 
03 字符串首字符大写
capitalize(self, /)
st03 = 'henan is very good'  
st = st03.capitalize() 
04 将字符串转换为便于比较格式
casefold(self, /)
st04 = 'ThewaetherIS Very HOt'  
st = st04.casefold()
05 字符串居中并填充
center(self, width, fillchar=’ ', /)
st05 = 'Welcome'  
st = st05.center(20,'-') 
06 查询子字符串在字符串中出现的次数,可以指定起止位置
S.count(sub[, start[, end]]) -> int
st06 = 'abcdefghijkabcdefghijk'  
st = st06.count('ka',1,-1)
07 将字符串按指定编码格式转为bytes类型
encode(self, /, encoding=‘utf-8’, errors=‘strict’)
st07 = '河南郑州'  
st = st07.encode('utf-8')  
08 判断字符串指定位置是否为子字符串
S.endswith(suffix[, start[, end]]) -> bool
st08 = 'Welcome'  
st = st08.endswith('come',3) 
09 将字符串中所有的\t扩展到指定位数长度
expandtabs(self, /, tabsize=8)
st09 = 'Welcome\tto\tBeiJing\t'  
st = st09.expandtabs(4) 
10 在字符串指定起止未知查找子字符串返回子字符串首次出现首字母的位置索引
S.find(sub[, start[, end]]) -> int
st10 = 'abcdefghijkabcdefghijk'  
st = st10.find('abc',10,-1)  
11 字符串格式化
S.format(*args, **kwargs) -> str
st11= '{},你好,欢迎来到{}.'  
st = st11.format('韩梅梅','北京')
st11 = '{1},你好,欢迎来到{0}.'  
st = st11.format('北京','韩梅梅') 
12 按关键字格式化字符串
S.format_map(mapping) -> str
st11 = '{name},你好,欢迎来到{city}.'  
st = st11.format(city='北京',name='韩梅梅') 
13 获取子字符串首次出现在指定起止未知的首字母的索引
S.index(sub[, start[, end]]) -> int
st13 = '韩梅梅,你好,欢迎来到北京.'  
st = st13.index('欢迎',1,-1)  
14 判断字符串是否全部由字母(保护汉字)和数字组成
isalnum(self, /)
st14 = '12345a和'  
st = st14.isalnum()
15 判断字符串是否全部由字母(保护汉字)
isalpha(self, /)
st15 = 'aaaa和'  
st = st15.isalpha() 
16 判断字符串是否全由ascii码组成
isascii(self, /)
st16 = '北京'  
st = st16.isascii() 
17 判断字符串是否全由数字组成
isdecimal(self, /)
st17 = '1234.1'  
st = st17.isdecimal()
18 判断字符串是否是数字字符串,不支持小数点
isdigit(self, /)
st18 = '1235'  
st = st18.isdigit()
19 判断字符串是否为python保留字
isidentifier(self, /)
st19 = 'def'  
st = st19.isidentifier()
20 判断字符串是否全部小写
islower(self, /)
st20 = 'beijing'  
st = st20.islower()  
21 判断字符串是否是数字字符串,不支持小数点
isnumeric(self, /)
st21 = '1212'  
st = st21.isnumeric()
22 判断字符串是不是一个可打印类型
isprintable(self, /)
st22 = str(set((1,2,3)))  
st = st22.isprintable() 
23 判断字符串是否为空字符串
isspace(self, /)
st23 = '    '  
st = st23.isspace()
24 判断字符串是否是标题类型
istitle(self, /)
st24 = 'The Title Is Henan'  
st = st24.istitle()  
25 判断字符串是否大写
isupper(self, /)
st25 = 'HELLOq'  
st = st25.isupper()
26 将一个可迭代对象用指定字符串连接起来
st26 = ('www','baidu','com')  
st = '.'.join(st26) 
27 指定字符串左对齐并显示指定的位数不够的位数用指定的字符串填充
ljust(self, width, fillchar=’ ', /)
st27 = 'Hello'  
st = st27.ljust(20,'-')  
28 将指定字符串小写
lower(self, /)
st28 = "Hello 韩梅梅,Welcome"  
st = st28.lower() 
29 移除字符串左边的自定字符串
lstrip(self, chars=None, /)
st29 = ' hello  welcome -'  
st = st29.lstrip('-')  
30 从字符串左侧查找指定分隔符,返回分隔符前、分隔符、分隔符后三部分的元祖
partition(self, sep, /)
st30 = 'hello,韩梅梅 welcome'
st = st30.partition(',')  
31 如果字符串以指定子串开头返回去除子串的部分,否则返回原字符串
removeprefix(self, prefix, /)
st31 = 'hello,韩梅梅 welcome'
st = st31.removeprefix('hello') 
32 如果字符串以指定子串结尾返回去除子串的部分,否则返回原字符串
removesuffix(self, suffix, /)
st32 = 'hello,韩梅梅 welcome'
st = st32.removesuffix('welcome') 
33 使用new子串替代字符串中的old子串
replace(self, old, new, count=-1, /)
st33 = 'hello,韩梅梅 welcome'
st = st33.replace('梅梅','雪雪') 
34 返回字符串中指定子串的最大索引
S.rfind(sub[, start[, end]]) -> int
st34 = 'abcdefghijkabcdefghijk'  
st = st34.rfind('d',1,-1)  
35 返回字符串中指定子串的最大索引
S.rindex(sub[, start[, end]]) -> int
st35 = 'abcdefghijkabcdefghijk'  
st = st35.rindex('d',1,-1)  
36 使字符串右对齐并显示指定长度,如果长度不够使用指定字符串填充
rjust(self, width, fillchar=’ ', /)
st36 = 'rjust with tab'  
st = st36.rjust(30,'-') 
37 从字符串右侧查找分隔符,使用分隔符将字符串分割为分割前、分隔符、分割后三部分的元祖
rpartition(self, sep, /)
st37 = '河南省信阳'  
st = st37.rpartition('省')  
38 用指定字符串从右侧分割字符串,返回分割后的字符串列表,可指定分割长度
rsplit(self, /, sep=None, maxsplit=-1)
st38 = 'hello,Lee,welcome,to,china'  
st = st38.rsplit(',',2)  
39 去除字符串右侧指定子串
rstrip(self, chars=None, /)
st39 = 'welcome --'  
st = st39.rstrip('-') 
40 用指定字符串从左侧分割字符串,返回分割后的字符串列表,可指定分割长度
split(self, /, sep=None, maxsplit=-1)
st40 = 'hello,Lee,welcome,to,china'  
st = st40.split(',',2)  
41 用换行符分割字符串,可指定显示换行符
splitlines(self, /, keepends=False)
st41 = 'Hello Lee \n Welcom to china'
st = st41.splitlines()  
42 判断字符串是否已子串开头
S.startswith(prefix[, start[, end]]) -> bool
st42 = 'helloleewelcometochina'  
st = st42.startswith('hello')
43 返回去除字符串左右指定子串后的字符串
strip(self, chars=None, /)
st43 = ' helloleewelcometochina '  
st = st43.strip(' ') 
44 是字符串中大小写字符互换
swapcase(self, /)
st44 = 'Hello Lee,welcome to china'  
st = st44.swapcase()
45 将字符串转为title类型字符串
title(self, /)
st45 = 'the title is test'  
st = st45.title() 
46 通过译码表对字符串中的字符进行一对一转换
translate(self, table, /)
st46 = 'ETC 快E速通过'  
ss = str.maketrans('ETC','电子记')  
st = st46.translate(ss) 
47 将字符串所有字符大写
upper(self, /)
st47 = 'the title is test'  
st = st47.upper() 
48 用0填充数字字符串的坐标构成指定长度
zfill(self, width, /)
st48 = '121.88'  
st = st48.zfill(12)
49 返回一个永远translate的译码表
maketrans(…)
st49 = str.maketrans('abc','ABC')  
st = st49 
50 字符串反转
st50 = 'chengdu'
st = st50[::-1]

更加详细说明请查考官方说明或使用help(str)查看说明文档。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值