常量,不可變型別,所有的修改都是返回乙個新的元素
str.split
根據指定引數進行切割,如果沒有匹配到切割引數則返回整個字串
rsplit表示從右往左切
str.split() 沒有引數,則已空格貪婪模式切割
str1 = 'asd \t \n fa sd f'
print(str1.split())
>>['asd', 'fa', 'sd', 'f']
str.split(' ') 如果指定引數為空格,則按照空格切
print(str1.split())
>>['asd', '', '', '', '', '', '', '', '\t', '', '', '\n', '', '', '', '', '', 'fa', '', '', '', '', 'sd', 'f']
str.split(' ',maxsplit=5) maxsplit指定最大切割次數
print(str1.split(' ',maxsplit=5))
>>['asd', '', '', '', '', ' \t \n fa sd f']
str.splitlines()
按照行進行切割,\r(mac) \r\n(win) \n(linux)都可識別
str1 = 'asd\t\nfa\rsd\r\nf'
print(str1.splitlines())
>>['asd\t', 'fa', 'sd', 'f']
str.partition()
根據指定引數進行一刀切,partition從左到右切,rpartition從右到左切
如果引數沒有匹配到則返回空
str2 = ''
print(str2.rpartition('/')[-1])
>>458302
str.replace()
替換,並返回乙個新的字串,如果引數沒有匹配到,則返回原來字串,可指定替換次數,預設全部替換
str2 = ''
print(str2.replace('course','math'))
>>
print(str2.replace('math','course'))
>>
str.strip() 兩頭去
str.startswith()、str.endswith()
返回bool值
判斷是否是指定參考開頭或者結尾
其他
Python資料型別 字串型別
變數名 str 變數值 msg hello world print msg 0 print msg 1 msg hello n print len msg msg hello world print ello in msg print lo w not in msg res print hello ...
Python資料型別 字串
字串 1 python 預設的檔案編碼都是ascii,所以要在編碼的時候加上coding utf 8,中文才不會亂碼。len 函式 是計算字串的長度。正確編碼的長度。b 中文 len b 長度是4 a 中文 decode gbk 或utf 8 print len a 長度是2 2 字串前加r 是不轉...
python資料型別(字串)
計算機人們日常事務的輔助工具,在程式設計中也映 現實世界的分類,因此計算機中也引入類別以便進行抽象分析 數字 字串 元組 列表 字典 int 表示的範圍 2,147,483,648 到 2,147,483,647 例如 0,100,100 num 2147483647 type num 一旦超出,則...