(1)字串取字元
(2)字串長度
(3)字串拼接
(4)字串切片**字串中的每乙個字元都有對應的下標(index),從0開始。
e.g.
>>>『hello world'[0]
'h'>>>'hello world'[2]
'l'>>>'hello world'[10]
'd'
e.g.
>>>'hello world'[11]
traceback (most recent call last):
file "", line 1, in indexerror: string index out of range
e.g.
>>>'hello world'[-1]
'd'>>>'hello world'[-3]
'r'>>>'hello world'[-11]
'h'
e.g.
>>>len(hello world)
11
兩個或更多個字串拼接成乙個長字串可以通過加法運算符號(+) 實現,也可以通過乘法運算子(*)實現。
e.g.
>>>'hello' + ' ' + 'world'
『hello world'
>>>'ok' * 3
'okokok'
>>>3 * 'ok'
'okokok'
>>>'ok' * 0
' ' #空串
非常重要,也是超級容易混亂的乙個操作。
其目的是取出字串的一部分,做成乙個新的字串。
描述切片的方式:
(s[m:n]) e.g.
>>>'hello world'[0:5]
'hello'
>>>'hello world'[:6]
'hello ' #省略m,從0開始,同時空格也佔乙個字元
>>>'hello world'[5:2]
' '>>>'hello world'[-9:-1]
'llo worl' #注意n=-1時,取不到s[-1]這個字元
>>>'hello world'[-9:]
'llo world'
(s[m:n:d]) e.g.
>>>'hello world'[::1]
'hello world'
>>>'hello world'[::-1]
'dlrow olleh'
>>>'hello world'[::2]
'hlowrd' #以2為步進
>>>'hello world'[1:9:1]
'ello wor'
>>>'hello world'[5:2:1]
' ' #空串
>>>'hello world'[3:8:-1]
' ' #空串
>>>'hello world'[-1::]
'd' #字串從s[-1]開始到最後乙個字元以1為步進
>>>'hello world'[:-1:]
'hello worl' #字串從s[0]開始到s[-1]以1為步進
end… python 字串使用
lower 將大寫字母全部轉為小寫字母。如 name g b name.lower title 將字串轉化為標題,即所有單詞的首字母大寫,其他字母小寫。使用方法同lower replace 返回某字串的所有匹配項均被替換之後得到的字串。this is a test replace is are sp...
python字串 Python 字串
建立字串很簡單,只要為變數分配乙個值即可。例如 var1 hello world var2 python runoob python訪問字串中的值python不支援單字元型別,單字元在 python 中也是作為乙個字串使用。python訪問子字串,可以使用方括號來擷取字串,如下例項 例項 pytho...
python字串 python字串
單引號示例常用的轉義字元 轉義字元案例1format 格式化練習1 help sisdigit,isnumeric,isdecimal 三個判斷數字的函式 isalnum 檢查字串是否由字母加數字組成 s test1split 字串拆分 splitlines 已換行符拆分 join 合成字串 upp...