首先,确保所需的数字是从开头或结尾开始的字符串 的有效索引 ,然后可以简单地使用数组下标表示法。用于
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>>>



