栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python字符串切片以及常见操作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python字符串切片以及常见操作

一、字符串的下标输出

模拟情景有这样一个字符串:myname = “dujunyan”

1. 取myname字符串的第1个元素,并输出

2. 取myname字符串的第5个元素,并输出

3. 取myname字符串的最后一个元素,并输出

4. 取myname字符串的倒数第二个元素,并输出

代码:

myname = 'dujunyan';

print(myname[0]);
print('————分界线————');
print(myname[4]);
print('————分界线————');
print(myname[-1]);
print('————分界线————');
print(myname[-2]);
print('————分界线————');

 结果:

二、字符串切片操作

讲解:

切片是指对操作的对象截取其中一部分的操作。 字符串、列表、元组都支持切片操作。 

切片的语法:[起始下标:结束下标:步长] (冒号是英文状态下的)

注意:选取的区间从"起始下标"位开始,到"结束下标"位的前一位结束(不包含结束位本身),步长表示选取间隔。

 下面我以字符串的形式为大家进行操作:

 代码:

myname = 'dujunyan';
#切片操作
print(myname[1:4:1]);#变量名[起始下标:结束下标:步长]
print('————分界线————');

结果:

以上面字符串为例,进行的操作,取字符串中的“uju”。

 三、字符串中的常见操作

1 、操作方法:find()

操作说明:检测“python”中是否包含在 my_str字符串中,如果包含返回开始的索引值,否则返回-1

代码:

my_str = 'hello world hello python';

print(my_str.find('python'));#结果1显示

print(my_str.find('s'));#结果2显示

结果1:

 结果2:

2、操作方法:index()

操作说明:检测 hello 是否包含在 my_str字符串中,如果包含则返回开始的索引值,否则报错

代码:

my_str = 'hello world hello python';

print(my_str.index('hello'));#包含“hello”结果1显示其下标,当找到了之后就不会再继续找

# print(my_str.index('s'));#字符串中没有包含“s”,所以结果2显示报错

结果1: 

结果2:

3、操作方法:count()

操作说明:返回“h”在start(头)和end(尾)之间 在 my_str字符串中里面出现的次数

代码:

my_str = 'hello world hello python';

print(my_str.count('h'));#结果1显示 3个

# print(my_str.count('s'));#结果2 没有则显示 0

结果1:

结果2:

4、操作方法:replace()

操作说明:把my_str字符串中的 “hello” 替换成 “666”,如果 count 指定,则替换不超过 count 次

代码:

my_str = 'hello world hello python';

print(my_str.replace('hello','666',2));#结果1显示

结果1:

5、操作方法:split()

操作说明:以空格为分隔符切片把my_str字符串进行分隔,如果 maxsplit有指定值,则仅分隔 maxsplit 个值字符串

代码:

my_str = 'hello world hello python';

print(my_str.split(" "));#结果1显示

print(my_str.split(" ",2));#结果2显示

结果1:

结果2: 

6、操作方法:startswith()

操作说明:检查字符串my_str中是否是以 h 开头, 是则返回 True,否则返回 False

代码:

my_str = 'hello world hello python';

print(my_str.startswith('h'));#结果1显示 True

# print(my_str.startswith('s'));#结果2 显示 False

结果1:

结果2:

7、操作方法:endswith()

操作说明:检查字符串my_str中是否以n结束,如果是返回True,否则返回 False

代码:

my_str = 'hello world hello python';

print(my_str.endswith('n'));#结果1 返回true

print('————分界线————')

# print(my_str.endswith('s'));#结果2 返回false

结果1:

结果2:

8、操作方法:upper()

操作说明:转换 my_str字符串中的小写字母为大写

代码:

my_str = 'hello world hello python';

print(my_str.upper());#结果1 显示

结果1:

9、操作方法:lower()

操作说明:转换 my_str字符串中所有大写字符为小写

代码:

my_str = 'Hello World Hello Python';

print(my_str.lower());#结果1显示

结果1:

10、操作方法:title()

操作说明:把字符串的每个单词首字母大写

代码:

my_str = 'hello world hello python';

print(my_str.title());#结果1显示

结果1:

11、操作方法:capitalize()

操作说明:把字符串的第一个字符大写

代码:

my_str = 'hello world hello python';

print(my_str.capitalize());#结果1显示

结果1:

12、操作方法:strip()

操作说明:删除my_str字符串两端的空白字符

代码:

my_str = '     hello world hello python     ';

print(my_str.strip());#结果1

结果1:

13、操作方法:rfind()

操作说明:类似于 find()函数,不过是从右边开始查找

代码:

my_str = 'hello world hello python';

print(my_str.rfind('world'))#结果1

结果1:

14、操作方法:join()

操作说明:每个字符后面插入“下划线”,使my_str字符串连接起来,构造出一个新的字符串

代码:

my_str = '_';

str = ['hello', 'world', 'hello', 'python'];

print(my_str.join(str));#结果1显示

结果1:

 以上就是Python中的字符串切片以及常见操作

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/269411.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号