字符串连接
- 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: