# 示例代码:使用 Python 截取字符串
# 定义一个字符串
text = "Hello, Python!"
# 使用切片操作截取字符串
# 语法:string[start:end:step]
# start: 开始索引(包含)
# end: 结束索引(不包含)
# step: 步长,默认为1
# 截取从索引2到索引5的子字符串(包含索引2,不包含索引5)
substring = text[2:5]
# 打印结果
print("截取的子字符串是:", substring)
# 输出:
# 截取的子字符串是: llo
text[2:5]
表示从索引2开始(包含),到索引5结束(不包含),因此返回的是 "llo"
。上一篇:python 文件存在
下一篇:python中chr函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站