返回

呼和浩特达内教育

Python字符串常用操作命令

python学习网更新时间:2021-11-04 浏览:96

本编码检测选用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 '


相关资讯

Python操作Excel教程:average函数求平均值的算法
2021-10-30 914
Python操作Excel教程-average函数求平均值...
人工智能和创客教育有什么区别
2022-11-05 843
人工智能和创客教育有什么区别对于人工智能,教育工作者首先要了解和接触这些技术,然后在课堂上介绍给学生。在这个过程中,教师...
怎么用热力图实现Python数据可视化
2021-10-28 703
大数据可视化是计算机科学或深度学习新项目中十分特别的一环。一般,你需要在工程前期开展探究性的数据统计分析(EDA),进而对信息有一定的掌握,并且建立数据可视化的确能够使剖析的目的更清楚...
人工智能在制造业的生产中可以发挥哪些作用
2021-06-12 445
人工智能在制造业的生产中可以发挥的作用共分为7项,分别是:1、预测性和预防性维护;2、提高机器人的效能;3、制造供应链;...
Python的基础语法
2021-11-03 431
编写Paython程序流程以前*对英语的语法有一定的掌握,才可以编写标准的Python程序流程...
二维密度图实现Python可视化的方法
2021-10-28 421
二维密度图(2D Density Plot)是一维版本号密度图的形象化拓展,相对性于一维版本号,其特点是可以见到有关2个自变量的概率分布函数...
Python操作Excel教程:提取Excel工作表的名字并修改的方法
2021-10-30 364
Python操作Excel教程-提取Excel工作表的名字并修改...
怎么运行python
2023-01-10 359
怎么运行python1. 使用Python自带的IDLE在开始--程序--Python2.5(视你安装的版本而不同)中找到IDLE(Python GUI),点击后弹出如下...

相关课程

栏目导航