python字符串切片常用方法有哪些

发布时间:2023-04-11 16:19:04 作者:iii
来源:亿速云 阅读:119

这篇文章主要介绍了python字符串切片常用方法有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python字符串切片常用方法有哪些文章都会有所收获,下面我们一起来看看吧。

一、切片

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

语法:序列[开始位置下标:结束位置下标:步长] ,不包含结束位置下标数据,步长为选取间隔,正负均可,默认为1

举例如下:

str = 'abcdefg_a'
print(str[1:6:2], str[2:6], str[:3], str[3:], str[:])
print(str[::2], str[:-2], str[-6:-2], str[::-2], str[::-1])
print(str[-2:], str[2:-2], str[-2::-2], str[:-2:2], str[2:-2:2])
 
输出:
bdf cdef abc defg_a abcdefg_a
acega abcdefg defg ageca a_gfedcba
_a cdefg _fdb aceg ceg

二、常用方法

2.1 查找

查找字符串:即查找子串在字符串中的位置或出现的次数

举例如下:

str = 'abcdefg_a'
print('-------------------查找-------------------')
print(str.find('c'), str.find('fg', 2, ), str.find('a', 2), str.find('h'))
print(str.index('c'), str.index('fg', 2, ), str.index('a', 2))
print(str.find('a'), str.rfind('a'), str.index('a'), str.rindex('a'), str.count('a'))
print(str.index('h'))
 
输出:
-------------------查找-------------------
2 5 8 -1
2 5 8
0 8 0 8 2
ValueError: substring not found

2.2 修改

修改字符串:通过函数形式修改字符串中的数据

举例如下:

print('--------------修改--------------')
str1 = 'hello python and hello IT and hello world and hello YX !'
print(str1.replace('and','&&'))
print(str1.split('and'), str1.split('and', 2))
l = ['Hello', 'world', '!']
t = ('Hello', 'python', '!')
print('_'.join(l), ' '.join(t))  # 用下划线_和空格连接
print(str1.capitalize())  # 首字符转为大写,其余均小写
print(str1.title())  # 每个单词首字母转为大写
str2 = '   Hello World !   '
print(str2.lower(), str2.upper(), str2.swapcase())  # 大写转小写,小写转大写,翻转大小写
print(str2.partition('rl'), str2.partition('o'))  # 根据指定分隔符将字符串分割,返回三元元组
print(min(str2), max(str2), ord(min(str2)), ord(max(str2)))  # str2中最小为空格对应十进制32,最大为r对应114
print(str2.zfill(21))  # 输出指定长度为21的字符串,右对齐,不足前面补0,超出指定长度则原样输出
print(str2.lstrip(), str2.rstrip(), str2.strip())  # 清除字符串左、右、两边空格字符
str3 = 'hello!'
print(str3.ljust(13, '*'), str3.rjust(13, '*'), str3.center(14, '*'))
 
输出:
--------------修改--------------
hello python && hello IT && hello world && hello YX !
['hello python ', ' hello IT ', ' hello world ', ' hello YX !'] ['hello python ', ' hello IT ', ' hello world and hello YX !']
Hello_world_! Hello python !
Hello python and hello it and hello world and hello yx !
Hello Python And Hello It And Hello World And Hello Yx !
   hello world !       HELLO WORLD !       hELLO wORLD !   
('   Hello Wo', 'rl', 'd !   ') ('   Hell', 'o', ' World !   ')
  r 32 114
00   Hello World !  
Hello World !       Hello World ! Hello World !
hello!******* *******hello! ****hello!****

2.3 判断

举例如下:

print('---------------判断----------------')
str3 = 'hello!'
print(str3.startswith('he'), str3.startswith('she'), str3.startswith('he',2,))
print(str3.endswith('!'), str3.endswith('。'), str3.endswith('!', 2, 5))
print(str3.isalpha(),str3.isalnum(), str3.isdigit(), str3.isspace())
 
输出:
---------------判断----------------
True False False
True False False
False False False False

关于“python字符串切片常用方法有哪些”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“python字符串切片常用方法有哪些”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. vscode连接python的方法
  2. VScode设置python环境的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:Android怎么使用socket进行二进制流数据传输

下一篇:Java怎么从本地文件复制到网络文件上传

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》