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

索引运算符【】

Doraemon6年前 (2020-02-16)Python6405

选择字符串的子序列

语法【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://world.myfpga.cn/index.php/post/130.html

分享给朋友:
返回列表

上一篇:for循环

下一篇:搜索字符串

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

0.Python环境的搭建

0.Python环境的搭建

   请打开网页  https://www.python.org/downloads/windows/    Windows环境下的Python        这里我选择...

1.Python基本的使用

1.Python基本的使用

我们打开python或者通过运行python也可以,请复制如下代码,然后按下Enter键,看看会发生什么?print('\n'.join([''.join([('MyFpga'[(x-y) % len('MyFpga'...

math库的使用

math库的使用

math库包括4个数学常数math.pi      圆周率math.e       自然对数math.inf     正无穷大,负无穷大为-math.infmath.nan     非浮点数标记math库常用函数math.cell(x)      向上取整,返回不小于x的最小整数math.facto...

random库

random库

random()            生成一个[0.0,1.0)之间的随机小数randint(a,b)     生成一个[a,b]之间的整数uniform(a,b)     生成一个[a,b]之间的随机小数对random库的引用方法与math库一样,采用下面两种方式实现:import random...

anaconda打不开的解决方法

anaconda打不开的解决方法

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

Python自动清理错误图片,深度学习训练数据集准备

Python自动清理错误图片,深度学习训练数据集准备

使用python运行from PIL import Image from pathlib import Path import os   path = r'.'  ...