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

索引运算符【】

Doraemon6年前 (2020-02-16)Python6364

选择字符串的子序列

语法【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循环

下一篇:搜索字符串

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

math库的使用

math库的使用

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

列表实例

列表实例

随机生成100个小写字母存入一个列表中,统计26个字母的出现次数。import random def getRandomLetter():     code_a=ord('a')     code_z=ord('z')     x=random.randint(code_a,code_z)...

列表作为函数参数

列表作为函数参数

列表作为函数参数,函数中可以修改原列表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 ...

(原创)使用Python对任意网站图片进行爬取,仅用于学习

(原创)使用Python对任意网站图片进行爬取,仅用于学习

import os import time import argparse import requests import re import io from urllib.parse import ...