假设我有一个由 x 个未知字符组成的字符串。我怎样才能得到 char nr. 13 或字符编号。 x-14?
首先确认需要的数量是有效索引对于从 begin 或 end 开始的字符串,那么您可以简单地使用数组下标表示法。
使用len(s)
获取字符串长度
>>> s = "python"
>>> s[3]
'h'
>>> s[6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> s[0]
'p'
>>> s[-1]
'n'
>>> s[-6]
'p'
>>> s[-7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)