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

索引运算符【】

Doraemon6年前 (2020-02-16)Python6776

选择字符串的子序列

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

下一篇:搜索字符串

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

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...

math库的使用

math库的使用

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

列表作为函数参数

列表作为函数参数

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

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

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

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

(原创)使用Python递归获取网页内的所有URL,并进行清洗

(原创)使用Python递归获取网页内的所有URL,并进行清洗

import argparse import time from urllib.parse import urljoin, urlparse from selenium import webdriver...