:::

2. 字串指令

>>> name = 'abcdefgh'
>>> print(type(name))
<class 'str'>
>>> name[3]
'd'
>>> name[2:4]
'cd'
>>> name[2:4:2]
'c'
>>> name[2:6:2]
'ce'
>>> name[::2]
'aceg'
>>> name[::-1]
'hgfedcba'
>>> name1=name.upper()
>>> name1
'ABCDEFGH'
>>> name
'abcdefgh'
>>> name.replace('def','XXX')
'abcXXXgh'
>>> len(name)
8
>>> 'g' in name
True
>>> 'y' in name
False
>>> 'abc' in name
True
>>> 'xyz' in name
False
>>> name.find('g')
6
>>> name.find('y')
-1


:::
http%3A%2F%2Fkang.phjh.hlc.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D6%26tbsn%3D3