python 操作字串最重要的8個方法

2021-08-19 19:38:26 字數 594 閱讀 7922

str.lower()和str.upper()輸出字串的小寫和大寫

str.split()返回乙個列表,例子:"a,b,c".split(",") 返回值為['a','b','c']

str.count(sub)返回乙個字串中sub出現的次數。

str.replace(old,new)字元替換函式,用new來替換old函式

str.center(width,"要填充的字元")

str.strip(chars) 去掉左側和右側chars列出的字元例子"= python= ".strip(" =np")結果為"ytho"

str.join(iter) 在iter變數中除最後乙個元素外每個元素增加乙個str,主要用於字串分割

例子",".join("12345") "1,2,3,4,5"

n*x表示複製n次x字串

len返回字串長度

str() 轉換為字元

hex(x)整數x的十六進製制表示

chr(u) 為unicode編碼,返回其對應的字元

ord(x) x為字元,返回其對應的unicode編碼

python3中所有的字串和**都是用unicode儲存的。

字串 最重要的基本匹配演算法KMP

includeusing namespace std int next 1005 len1,len2 char str 1005 sub str 1005 void kmp int k 1,i 0 next 0 1 while i str sub str len1 strlen str len2 s...

Alex學Ruby 最重要的資料型別 字串

但我還記得c語言裡的字串是以空字元結束的,但 ruby不是,所以,ruby的字串裡可以包含空字元。2.ruby裡的字串分為兩種,其中最簡單的一種是用單引號括起的字串,表示的完全是字面意義,唯一支援的轉義序列是 單引號和反斜槓 str i m ok a u ok xie i m ok a u ok x...

Python字串操作

1 複製字串 str2 str1 2 鏈結字串 str abc 3 查詢字串 string.find sub string.index sub string.rfind sub string,rindex sub 4 字串比較 cmp str1,str2 cmp str1.upper str2.up...