python—字符串常用方法
| 方法及使用 | 描述 | 说明 |
| str.lower()或str.upper() |
返回字符串的副本,全部字符小写/大写 “AaaBBcc”.lower()结果为“aaabbcc” |
lower全部转换为小写 upper全部转换为大写 |
| str.split(sep=None) |
返回一个列表,由str根据sep被分隔的部分组成 “A,B,C”.split(",")结果为【’A','B','C'】 |
以逗号为分割,组成列表 |
| str.count(sub) |
返回子串sub在str中出现的次数 "an apple a day".count("a")结果为4 |
计数器,总共出现的次数 |
| str.replace(old,new) |
返回字符串str副本,所有old子串被替换为new “python”.replace("n","n123.io")结果为“python123.io” |
替换字符串 |
| str.center(width,[fillchar]) |
字符串str根据宽度width居中,fillchar可选 "python".center(20,"=")结果为'====python=====' |
以20个字符的宽度让字符串居中,两边以=号填充 |
| str.strip(chars) |
从str中去掉其左侧和右侧chars中列出的字条 “= python=”.strip("= np")结果“ytho" |
常用的是去掉两边空格及其它符号 |
| str,join(iter) |
在iter变量除最后元素外,每个元素后增加一个str ”,“.join("1234")结果”1,2,3,4“ |
主要用于字符串分隔 |

浙公网安备 33010602011771号