Python字符串運算符


Python字符串運算符:

  + :連接左右兩端的字符串。

  *  :重復輸出字符串。

  [ ] :通過索引獲取字符串中的值。

  [start:stop:step]:開始,結束位置的后一個位置,步長。

  in  :判斷左端的字符是否在右面的序列中。

  not in:判斷左端的字符是否不在右面的序列中。

  r/R :在字符串開頭使用,使轉義字符失效。

# 字符串使用 + 號 strs = "hello " + "world." print(strs) # hello world. # 字符串使用 * 號 strs = 'abc ' # 無論數字在哪一端都可以 print(3*strs) # abc abc abc print(strs * 3) # abc abc abc # 使用索引下標 strs = "hello world." print(strs[4]) # o print(strs[7]) # o # 切片操作,左閉右開原則 strs = "hello world." # 將字符串倒序輸出 print(strs[::-1]) # .dlrow olleh print(strs[6:11:]) # world  strs = "ABCDEFG" print("D" in strs) # True print("L" in strs) # False print("D" not in strs) # False print("L" not in strs) # True # 使用 r 使字符串中的轉義字符失效 print('a\tb') # a b print(r'a\tb') # a\tb

2020-02-08


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM