python面...

python面试一《字符串》:

2016-11-16  本文已影响27人  jackbom

1.python没有char数据类型。

2.字符串切片:

正索引:a[:],a[:3],a[2:],a[1,8,2]

负索引:a[-1],a[-2],a[:-2],a[-2:-1],a[:-1:-1]

正索引越界取最后一个元素

负索引越接会出错

不过需要注意的是 -0 实际上就是 0,所以它不会从右边开始计数!

>>> word[-0]'H'# (since -0 equals 0)Out-of-range negative slice indices are truncated, but don't try this for single-element (non-slice) indices:负索引切片越界会被截断,不要尝试将它用于单元素(非切片)检索

>>> word[-100:]'HelpA'

>>> word[-10]# errorTraceback (most recent call last):File "", line 1, in ?

IndexError: string index out of range

3.Python 字符串不可变。向字符串文本的某一个索引赋值会引 发错误

>>> word[0] = 'x'Traceback (most recent call last):File "", line 1, in ?

TypeError: object does not support item assignmen

上一篇下一篇

猜你喜欢

热点阅读