当前位置:首页 > Software > Python > 正文内容

索引运算符【】

Doraemon6年前 (2020-02-16)Python5689

选择字符串的子序列

语法【start:finish】

        start:子序列开始位置的索引值

        finish:子序列结束位置的下一个字符的索引值

如果不提供start或者finish,默认start为第一个字符,finish为最后一个字符。

例如

>>>my_str='hello world'
>>>my_str[6:10]
'worl'
>>>my_str[6:]
'world'
>>>my_str[:6]
'hello'
>>>

接收三个参数

start:finish:countBy

默认countBy为1

>>>my_str='hello world'
>>>my_str[0:11:2]
'hlowrd'

获得逆字符串

>>>my_str='spam'
>>>reverse_str=my_str[::-1]
>>>print(reverse_str)
maps


扫描二维码推送至手机访问。

版权声明:本文由我的FPGA发布,如需转载请注明出处。

本文链接:https://myfpga.cn/index.php/post/130.html

分享给朋友:
返回列表

上一篇:for循环

下一篇:搜索字符串

“ 索引运算符【】” 的相关文章

2.Python中的基本运算

2.Python中的基本运算

我们打开Python,请你尝试输入如下算式并尝试理解有什么为什么是这样的?1+1 1+1.0 1-2 2-3.5 1*1 1*1.1 1/2 2/1 2/3 3/2 3//2 3/1.0 5/2.5我们不难得到如下结果2 2.0 -1 -1.5 1 1.1 0.5...

搜索字符串

搜索字符串

常用搜索字符串中子串的方法str.count(substring)      返回str中substring子串出现的无覆盖的次数str.find(s1)                    返回s1在这个字符串的最低下标,如果字符串中不存在s1,则返回-1str.rfind(s1)       ...

列表作为函数参数

列表作为函数参数

列表作为函数参数,函数中可以修改原列表def multiply(values,factor):     for i in range(len(values)):        values[i]*=factor          aList=[1,2,3,4,5]  multiply(aL...

anaconda打不开的解决方法

anaconda打不开的解决方法

报错内容Navigator Error An unexpected error occurred on Navigator start-up Report Please report this ...