字符串可以用單引號,雙引號,三引號成對括起來,索引用[]界定,切片用[:]來省略和連接,這點類似於Matlab。
>>> s1='Python'
>>> s2=' is cool!'
>>> str=s1+s2
>>> str
'Python is cool!'
>>> str[0]
'P'
>>> str[2:5]
'tho'
>>> str[:2]
'Py'
>>> str[3:]
'hon is cool!'
>>> str*2
'Python is cool!Python is cool!'
>>> '-'*8
'--------'
>>> str[-1]
'!'
字符串下標從0開始,最後一個字符的索引是-1。加號來連接,*用來重複。>>> str2='Python\n is cool!'
>>> str2
'Python\n is cool!'
>>> print str2
Python
is cool!