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