python字符串常用方法
1、这里打开编辑器sub盟敢势袂lime text3作为示范,新建一个py文档。test = "apple"pri荏鱿胫协nt(test.capitalize())capitalize()可以把英语变为大写。
2、test = "apple"print(test.count("p"))如果需要数一下某个字符串出现的次数,可以用.count()。
3、test = " apple"print(test.lstrip())test = "apple "print(test.rstrip())lstrip()和rstrip()分别是可以去除左边和右边的空格。
4、test = "apple"print(test.index("p"))index()可以找到字符串的序号,多个的时候会返回第一个。
5、test = "apple"print(test.replace("p", "b"))replace()可以替换字符串。
6、test = "apple"print("The {}".format(test)).format()是格式化字符串输出。
7、test = "apple"print(test.startswith("b"))pri荏鱿胫协nt(test.endswith("le"))startswith()和endswith()分别判断字符串是否以什么开头或者结尾。
8、test = "apple orange"print(test.split())split()可以把字符串变为列表。
9、test = " ".join(['apple', 'orange'])print(test)"".join()则是把列表更变为字符串。
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:52
阅读量:71
阅读量:38
阅读量:46
阅读量:90