Python:字符串函数

String模块中的常量:

string.digits:数字0~9

string.letters:所有字母(大小写)

string.lowercase:所有小写字母

string.printable:可打印字符的字符串

string.punctuation:所有标点

string.uppercase:所有大写字母

>>> import string
>>> string.digits
'0123456789'
>>> string.letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> string.uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


1、find函数

在一个较长的字符串中查询子字符串,返回子串所在位置最左端索引,没有找到返回-1

>>> title = "Monty Python's Flying Circus"
>>> title.find('Monty')
0
>>> title.find('monty')
-1
可以选择起始点和结束点

>>> title.find('Python')
6
>>> title.find('Python', 3)
6
>>> title.find('Python', 3, 10)
-1

2、join函数

在队列中添加元素(只能操作于字符串,返回一个修改后的字符串,但是原字符串不改变)

>>> seq = ['1', '2', '3', '4', '5']
>>> sep = '+'
>>> sep.join(seq)
'1+2+3+4+5'
>>> seq
['1', '2', '3', '4', '5']
>>> dirs = '', 'usr', 'bin', 'env'
>>> '/'.join(dirs)
'/usr/bin/env'
>>> print 'C:' + '\\'.join(dirs)
C:\usr\bin\env

逆方法:split函数

将字符串分割成序列,返回该序列,原字符串不改变

>>> word = '1+2+3+4+5'
>>> word.split('+')
['1', '2', '3', '4', '5']
>>> word
'1+2+3+4+5'

3、lower函数

返回字符串的小写字母版

>>> 'fafDAWdfaweDWED'.lower()
'fafdawdfawedwed'
扩展:

title函数:首字母大写,其他小写

>>> "that's all folks".title()
"That'S All Folks"
capwords函数:功能同上,为string模块中函数

>>> import string
>>> string.capwords("that's all folks")
"That's All Folks"

4、replace函数

返回某字符串所有匹配项均被替换之后得到的字符串,原字符串不改变

>>> word = 'this is a test'
>>> word.replace('is', 'eez')
'theez eez a test'
>>> word
'this is a test'

maketrans函数:功能同上,string中的转换表,共有256个项目,函数接受2个等长的字符串,第一个字符串中的每个字符都用第二个字符串中相应位置的字符来进行替换

maketrans类似于一种规则,经常与translate结合,以完成一些普通函数无法完成的字符串替换

>>> from string import maketrans
>>> table = maketrans('cs', 'kz')
>>> len(table)
256
>>> table[97:123]
'abkdefghijklmnopqrztuvwxyz'
>>> maketrans('','')[97:123]
'abcdefghijklmnopqrstuvwxyz'

translate函数:功能同上,但是只能处理单个字符,有2个参数,第一个是替换,第二个是删除

例:table承继maketrans中的table

>>> 'this is an incredible test'.translate(table)
'thiz iz an inkredible tezt'
>>> 'this is an incredible test'.translate(table, ' ')
'thizizaninkredibletezt'


5、strip函数

去除两侧(不包括内部)空格的字符串,原序列不变

>>> word = '   this is test    '
>>> word.strip()
'this is test'
>>> word
'   this is test    '
可在strip()加入参数,以去除想要去掉的指定字符

>>> '***  SPAM  *  for  *  everyone!!!  ***'.strip('*')
'  SPAM  *  for  *  everyone!!!  '
>>> '***  SPAM  *  for  *  everyone!!!  ***'.strip('* ')
'SPAM  *  for  *  everyone!!!'
>>> '***  SPAM  *  for  *  everyone!!!  ***'.strip('* !')
'SPAM  *  for  *  everyone'




  • 4
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python提供了很多字符串函数来处理和操作字符串。下面是一些常用的字符串函数: 1. capitalize()函数:将字符串的首字母大写,其余字母小写。例如,"i love python"经过capitalize()函数处理后,结果为"I love python"。 2. center()函数:将字符串居中,可以指定字符串的长度和填充的字符。例如,"i love python"经过center(20, "*")函数处理后,结果为"***i love python****"。 3. ljust()函数:将字符串左对齐,可以指定字符串的长度和填充的字符。例如,"i love python"经过ljust(20, "8")函数处理后,结果为"i love python8888"。 4. title()函数:将字符串中每个单词的首字母大写,其余字母小写。例如,"i love python"经过title()函数处理后,结果为"I Love Python"。 5. swapcase()函数:将字符串中的大写字母转换为小写字母,小写字母转换为大写字母。例如,"i love python"经过swapcase()函数处理后,结果为"I LOVE PYTHON"。 这些函数可以根据需要在程序中使用,对字符串进行各种处理和操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python字符串函数用法大全](https://blog.csdn.net/xiaozhiamy/article/details/104944012)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值