Python字符串函数大全系列文章(一)

1.capitalize()函数: 将原始字符串规范化为首字母大写,其他字母小写的字符串。

>>> s1 = "hello world"
>>> s1.capitalize()
'Hello world'
>>> s2 = 'hello WoRLd'
>>> s2.capitalize()
'Hello world'

2.casefold()函数: 消除大小写差异,统一变为小写。

>>> s1 = 'HelLo woRld'
>>> s1.casefold()
'hello world'

3.center(width, fillchar=’ ', /)函数:将原始字符串填充至指定宽度,原始字符串位于中间,填充符位于两边;默认以空格填充,也可以指定填充字符;如果指定宽度小于原始字符串,不会产生任何效果。

# 指定宽度和填充符
>>> s1 = 'python'
>>> s1.center(10,'*')
'**python**'

# 指定宽度,不指定填充符
>>> s1.center(10)
'  python  '

# 指定宽度小于原始字符串长度
>>> s1.center(3,'*')
'python'

4.count()函数: 返回某个子字符串的个数,具体参数形式为:(sub[, start[, end]]),sub表示子字符串,中括号里面的参数是可选的切片参数,可传可不传。

>>> s1 = 'this is a good job, is it' # 长度25

# 不指定切片
>>> s1.count('is')
3

# 指定start,不指定end,则切片范围从start一直到末尾
>>> s1.count('is',2)
3
>>> s1.count('is',4)
2

# 同时指定start,end,切片范围左闭右开[start,end),不包括end本身
>>> s1.count('is',2,22)
3
>>> s1.count('is',2,21)
2

5.encode()函数: 将字符串编码,默认 utf-8

s1 = "hello python"
s2 = s1.encode()
type(s2)
bytes

6.endswith()函数: 判断某字符串是否以某后缀结尾

s1 = "hello python"
s2 = s1.endswith('n')
s2
True

7.expandtabs(): 将 ‘\t’ 用若干空格代替,默认是8


s1 = "hello python\t"
s2 = s1.expandtabs(4)
s2
'hello python    '

8.find(): 返回子字符串第一次出现的位置,如果没有,则返回-1


s1 = "hello world"
s2 = s1.find('wd')
s2
-1

9.format() : 格式化字符串,通俗点,就是将字符串变成你想要的格式,主要掌握以下几种常用用法
1): 将原始字符串转化为你想要的任意格式的字符串

>>> a = 'hello'
>>> b = 'python'
>>> '{} {}'.format(a,b) #{}是占位符
'hello python'

#你可能会说上面那种情况,直接拼接就行了,搞那么复杂?
#因为format 中间可以加任意字符,可读性比拼接更强
>>> '{} ****&&&***** {}'.format(a,b)
'hello ****&&&***** python'

#下面例子更体现format的灵活性,{0}代表a,{1}代表b
>>> '{0} $$ {1} && {1} ** {0}'.format(a,b)
'hello $$ python && python ** hello'

# 如果你觉得{0},{1}可读性好像不是还不够强,那还可以加名字
>>> '姓名:{name}   年龄: {age}'.format(name='xiaoming',age=18)
'姓名:xiaoming   年龄: 18'

#你又会想,上面指定name和age的方式能不能更灵活,完全可以
#可以指定一个列表传进去,这样可以实现统一配置
>>> proper_list = ['xiaoming',18] #这个list可以通过配置文件生成
>>> '姓名:{0[0]}   年龄: {0[1]}'.format(proper_list)
'姓名:xiaoming   年龄: 18'

#除了指定列表,其实还可以指定字典
>>> proper_dic = {'name':'xiaoming','age':18}
>>> '姓名:{name}, 年龄:{age}'.format(**proper_dic)# ** 表示对字典解包
'姓名:xiaoming, 年龄:18'

2): 保留两位小数,百分比

>>> '{:.2f}'.format(56.87675858)
'56.88'

#转化为百分比
>>> '{:.2%}'.format(56.87675858)
'5687.68%'

3): 左边或右边补0,这个技巧可能AI预处理有用

# > 表示右对齐,即左边补0,d表示宽度8位
>>> '{:0>8d}'.format(56)
'00000056'
>>> '{:0<8d}'.format(56)
'56000000' #右边补0

4): 进制转换,将十进制121转换为其他进制

>>> '{:b}'.format(121)  # 二进制
'1111001'  
>>> '{:#o}'.format(121)  # 八进制
'171'
>>> '{:x}'.format(121)  # 十六进制
'79'
>>> '{:#x}'.format(121) # 十六进制,前面加x
'0x79'
>>> '{:d}'.format(121)   #十进制
'121' 

10.format_map():简化了通过字典格式化字符串的方式

>>> dic = {'name':'小明','score':150,'age':18}
>>> desc_str = '那个人叫{name},年龄{age}岁,数学考了{score}分'
>>> ret_str = desc_str.format_map(dic)
>>> ret_str
'那个人叫小明,年龄18岁,数学考了150分'

11.index(): 返回子字符串第一次出现的位置,如果没有,会报错

>>> s1 = "hello world"
>>> s2 = s1.index('wo')
>>> s2
6

12.isalnum(): 判断某字符串是否为字母或数字组成的字符串

>>> s1 = "5656"
>>> s2 = s1.isalnum()
>>> s2
True
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zack爱编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值