2.4 python中的字符串、索引和截取 [python入門教程]

        字符串可以用單引號,雙引號,三引號成對括起來,索引用[]界定,切片用[:]來省略和連接,這點類似於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!


發佈了26 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章