""" 字符串的基本操作 """ import operator s = ' hello, world! ' # 去掉空格/换行符 s.strip() # 从左侧切掉 s.lstrip(' hello, ') # 从右测切掉 a = s.rstrip('world! ') # 字条串拼接 s2 = 'to me ' a = s + s2 # 查找第一个出现该字符的位置 a = s.index('o') a = s.find('o') # 查找该字符出现的次数 a = s.count('o') # 字符串的比较, 该为py3的用法 a = operator.eq(s, s) # 字母转大写 a = s.upper() # 字母转小写 a = a.lower() # 翻转字符串 a = s[::-1] # 切片 a = s.split(',') # 右侧切片 a = s.rsplit()
python 字符串基本操作
最新推荐文章于 2022-05-15 21:11:08 发布