python中的切片使用slice进行操作,标准格式(包头不包尾)
[起始偏移量start:终止偏移量end:步长step]
典型操作
其他操作(三个量为负数)的情况:
切片操作时,起始偏移量和终止偏移量不在[0,字符串长度-1]这个范围,也不会报错。起始偏移量小于0则会当做0,终止偏移量大于“长度-1”会被当成-1。例如:
>>> "abcdefg"[3:50]
'defg'
- 将”to be or not to be”字符串倒序输出
``
a="to be or not to be
b=a[::-1]
print(b)
eb ot ton ro eb ot
将”sxtsxtsxtsxtsxt”字符串中所有的s输出
```python
a=”yscyscyscysc“
b=a[::3]
print(b)
yyyy