• 手机站
  • 收藏
聚培教育网合作机构 > 呼和浩特达内教育
呼和浩特达内教育
400-998-6158
呼和浩特达内教育开设Java、C++、C#/.Net、 PHP、嵌入式、软件测试、UID、网络营销、Linux云计算、主办会计、UED、WEB前端等课程
呼和浩特达内教育

Python字符串常用操作命令

python学习网

更新时间:2021-11-04 浏览:100
核心提示:本编码检测选用python3编译器,find string = "i love python very much " 查验字符串数组是不是包括在string中,假如包括则回到字符串数组逐渐的字符(数据库索引)

本编码检测选用python3编译器

1.find string = "i love python very much " 查验字符串数组是不是包括在string中,假如包括则回到字符串数组逐渐的字符(数据库索引),假如不包含则回到-1

string = "i love python very much "

string.find("love")

2

string.find("study")

-1

rfind 和find相近,但是是以右边开始查找,由于有的字符串数组在原字符串数组中也有好几个,但传参仅有**个,全部rfind是归属于右边优先选择标准

2.index 功效和find一样,但假如不包含字符串数组则抛出现异常(出错),rindex和rfind相近

string = "i love python very much "

string.index("love")

2

string.index("study")

Traceback (most recent call last):

    File "", line 1, inValueError: substring not found

3.count 回到字符串数组在总体目标字符串数组中产生的频次

string = "i love python very much "

string.count("o")

2

string.count("python")

1

string.count(" ")

5

string.count("study")

0

4.replace 将字符串数组中规定的字符串数组用其他字符串数组开展更换

string = "i love python very much "result1 = string.replace("python","girl")

result1

'i love girl very much '#行内编码中的2指得是数最多应用—_更换2个空格符,第三个主要参数不写默认设置为0

result2 = string.replace(" ","_",2)

result2

'i_love_python very much '

5.split 以特定标识符切分切成片字符串数组,传参为list目录,无主要参数默认设置为空格符切分

string = "i love python very much "#不写主要参数默认设置应用空格符切分,且持续好几个空格符视作一个#若空格符在**部或尾端则不会再往两边切分result3 = string.split()

result3

['i', 'love', 'python', 'very', 'much']

result4 = string.split(" ")

result4

['i', 'love', 'python', 'very', 'much', '']

result5 = string.split("e")

result5

['i lov', ' python v', 'ry much ']

6.capitalize 将字符串数组首写转化成英文大写

string = "i love python very much "result6 = string.capitalize()

result6

'I love python very much '

7.title 将字符串数组中各个英语单词的首写

result7 = string.title()

result7'I Love Python Very Much '

8.starswith 查验字符串数组开始是不是包括特定字符串数组

string = "i love python very much "result9 = string.startswith("i")

result9

True

result10 = string.startswith("a")

result10

False

9.endswith 查验字符串数组末尾是不是包括特定字符串数组,和startswith相近

string = "i love python very much "result11 = string.endswith(" ")

result11

True

result12 = string.endswith("ab")

result12

False

10.lower 将字符串数组中全部的英文大写字母转换为小写字母

string = "i love python very much"result13 = string.lower()

result13'i love python very much'

11.upper 将字符串数组中全部的英文字母转换为英文大写,和lower反过来的功效

string = "i love python very much"result14 = string.upper()

result14'I LOVE PYTHON VERY MUCH'

12.ljust 回到一个原字符串数组左两端对齐,并应用空格符来添充剩下地方的字符串数组

string =" hello"result15 = string.ljust(10)

result15

'hello '

13.rjust 回到一个原字符串数组右两端对齐,并应用空格符来添充剩下地方的字符串数组

string =" hello"result16 = string.rjust(10)

result16

' hello'

14.center 回到一个原字符串数组垂直居中两端对齐,并应用空格符来添充剩下地方的字符串数组

string =" hello"result17 = string.center(9)

result17

' hello '


更多>同类资讯
更多>相关课程
顶部