首页 > 编程语言 > 详细

Python字符串的截取原理,下标的位置图示

时间:2019-09-22 18:58:47      阅读:121      评论:0      收藏:0      [点我收藏+]

Python字符串截取时总是有些糊涂,从官网上找到一个图示,理解Python字符串是如何标记,的具体含义图示如下:

 

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

 

>>> word[:2]  # character from the beginning to position 2 (excluded)
‘Py‘
>>> word[4:]  # characters from position 4 (included) to the end
‘on‘
>>> word[-2:] # characters from the second-last (included) to the end
‘on‘

 

Python字符串的截取原理,下标的位置图示

原文:https://www.cnblogs.com/frisk/p/11568441.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!