str()和repr()函数 # 数值转换为字符串
dir() # 列出指定类或模块包含的全部内容
help() # 查看某个函数或方法的帮助文档
s.title() # 将每个单词首字母大写
s.lower() # 将整个字符串小写
s.upper() # 将整个字符串大写
s.strip() # 删除字符串左右空白
s.lstrip() # 删除字符串左边空白
s.rstrip() # 删除字符串右边空白
s.strip('xxx') # 删除字符串左右任意字符
s.lstrip('xxx') # 删除字符串左边任意字符
s.rstrip('xxx') # 删除字符串右边任意字符
s.startswith('xxx') # 判断字符串是否以指定子串开头
s.endswith('xxx') # 判断字符串是否以指定子串结尾
s.find('xxx') # 查找指定子串在字符串中的位置,返回起始位置,如果不存在,返回-1
s.index('xxx') # 查找指定子串在字符串中的位置,返回起始位置,如果不存在,引发ValueError错误
s.replace('xx','yy', count) # 用yy替换字符串中的xx,count为替换个数,如果省略就是全部替换
s.translate() # 根据指定翻译映射表对字符串执行替换
s.split() # 将字符串分割为多个短语,默认分隔符为空格
'x'.join() # 以x为拼接符将多个短语拼接成字符串
举例:s.translate()
s= "hello hello hello"
table = str.maketrans("ho", "n ")
s = s.translate(table)
print(s)
# 返回为
nell nell nell
举例:s.split() 和 ’ '.join()
s= "hello hello hello"
s_list = s.split()
print(s_list)
print(' '.join(s_list))
print('! '.join(s_list))
#返回为
['hello', 'hello', 'hello']
hello hello hello
hello! hello! hello
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)