python字符串常用方法

 时间:2024-11-01 17:17:58

1、这里打开编辑器sublime text3作为示范,新建一个py文档。test = "apple"print(test.capitalize())capitalize()可以把英语变为大写。

python字符串常用方法

2、test = "apple"print(test.count("p"))如果需要数一下某个字符串出现的次数,可以用.count()。

python字符串常用方法

3、test = " apple"print(test.lstrip())test = "apple "print(test.rstrip())lstrip()和rstrip()分别是可以去除左边和右边的空格。

python字符串常用方法

4、test = "apple"print(test.index("p"))index()可以找到字符串的序号,多个的时候会返回第一个。

python字符串常用方法

5、test = "apple"print(test.replace("p", "b"))replace()可以替换字符串。

python字符串常用方法

6、test = "apple"print("The {}".format(test)).format()是格式化字符串输出。

python字符串常用方法

7、test = "apple"print(test.startswith("b"))print(test.endswith("le"))startswith()和endswith()分别判断字符串是否以什么开头或者结尾。

python字符串常用方法

8、test = "apple orange"print(test.split())split()可以把字符串变为列表。

python字符串常用方法

9、test = " ".join(['apple', 'orange'])print(test)"".join()则是把列表更变为字符串。

python字符串常用方法
  • python中怎样对特殊字符进行转义?
  • array_replace()使后面数组替换第一个数组的值
  • Python的MySQLdb模块安装和使用
  • 如何用visual studio创建C++语言的除法程序
  • 如何使用python的pygal模块画饼状图
  • 热门搜索
    凉拌腐竹的做法 干锅鱼的做法 垠怎么读 米饭的做法 手撕包菜做法 韭菜的做法大全 牛肉粒的做法 茄子的做法大全家常 提拉米苏的做法 韭菜猪肉饺子馅的做法