下列包含Pyhton3中字符串相关内置函数,丢丢哥抽时间罗列了一些;主要用于速查而保存记录
一、字母相关处理
str = "My Name is DiuDiu"
print(str.upper()) # 全部大写 MY NAME IS DIUDIU
print(str.lower()) # 全部小写 my name is diudiu
print(str.swapcase()) # 大小写互换 mY nAME IS dIUdIU
print(str.capitalize()) # 首字母大写,其余小写 My name is diudiu
print(str.title()) # 每段首字母大写 My Name Is Diudiu
print(max(str)) # 返回字符串 str 中最大的字母 y
print(min(str)) # 返回字符串 str 中最大的字母
二、字符串搜索相关处理
where = "I`m from China !"
print(where.find('a', 1, 14)) # 搜索指定字符串,没有返回-1 13
print(where.index('f', 1, 13)) # 同上,但是找不到会报错 4
print(where.rfind('f')) # 从右边开始查找 4
print(where.count('m')) # 统计指定的字符串出现的次数 2
三、字符串替换相关
word = "So far, when dealing with collections, you've seen for/ in used with a generic variable type, like Object."
print(word.replace('like', 'LOVE')) # like 替换 LOVE
print(word.replace('with', 'WITH', 2)) # with 替换两次 WITH
四、字符串去空格及去指定字符
forms = " Such lists are/long/and/tedious to read. "
print(forms.lstrip()) # 去左边空格
print(forms.rstrip()) # 去右边空格
print(forms.strip()) # 去两边空格
print(forms.split()) # 默认按空格分割成数组 ['Such', 'lists', 'are-long-and-tedious', 'to', 'read.']
print(forms.split('/')) # [' Such lists are', 'long', 'and', 'tedious to read. ']
五、字符串判断相关
bey = "Reaction rates o-bey the parabolic line law"
print(bey.startswith('read')) # 是否以read开头 False
print(bey.endswith('law')) # 是否以law结束 True
print(bey.isalnum()) # 是否全为数字或字母 False
print(bey.isalpha()) # 是否为全字母 False
print(bey.islower()) # 是否为全小写 False
print(bey.isupper()) # 是否为全大写 False
print(bey.istitle()) # 判断首字母是否为大写(所有空格前大写) False
print(bey.isspace()) # 判断字符串是否为空格 False
六、其他相关
num = 1000
print(bin(num)) # 十进制转八进制
print(hex(num)) # 十进制转十六进制
print(type(bey)) # 查看数据类型
print(len(bey)) # 字符串长度
print(format(bey)) # 格式化字符串
print(abs(-100)) # 绝对值 100