python常用方法及函数

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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值