python——操作字符串

字符串连接

  • join()
    该方法用于连接字符串数组
s=['a','b','c']
print('-'.join(s))
  • 用字符串占位符进行替换
s=['a','d','j']
content='%s%s%s' % tuple(s)
print(content)

字符串分割

  • split()
    该方法返回一个字符串列表
    基本格式为:str.split(‘str’,num)
    参数中:str为分隔符,默认为空格,num为限制的当前分割数量
str='hello world aaa'
print(str.split())
pritn(str.split(' ',1))
  • findall()
    该方法可实现用多个分隔符对字符串进行分割
import re
s='hey ,you-what are you doing here!'
print(re.findall(r'[\w]+',s))

字符串替换

  • replace()
s='hello world'
pritn(s.replace('world','python'))
  • 匹配正则
import re
s='hello world'
strinfo=re.compile('world')
print(strinfo.sub('python',s))

字符串查找

  • find()
    该方法实现在字符串中查找子串,如果找到就返回子串所在下标,没有找到则返回-1

  • in
    该方法不仅可用于列表,元祖等数据类型,也可用于字符串
    for i in str:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值