????????????????????????????????????????????????Python入門之字符串
一、字符串的概念及創建
1、字符串的概念
* 在程序中,文本內容用字符串來表示
* 字符串是有一系列有序的字符組成,如: 'helloworld'
* 字符串和列表,元組一樣,都屬于序列類型'
* 可以將字符串看做字符的列表,列表的很多操作對于字符串也是適用的
* 沒有單獨的字符類型,字符就是指包含一個元素的字符串 例如: 'a', 'b', 'c'
2、字符串的創建
''' 使用雙引號或者單引號都可以創建字符串 ''' s?=?'ok' print?(s,type(s))?#?ok?<class?'str'> s1?=?"ok1" print?(s1,type(s1))?#?ok1?<class?'str'> ''' 使用內置函數str,傳入的可以是數字,可以是字母,也可以是浮點數,最終該數據類型為str類型 ''' s2?=?str('abc') print?(s2,type(s2))?#?abc?<class?'str'> s3?=?str('123') print?(s3,type(s3))?#?123?<class?'str'>
?
二、轉義字符
1、使用轉義字符無法表示的特殊字符
當字符串中包含換行、回車、水平制表符、退格等無法直接表示的特殊字符時,該如何表示呢?
????換行: newline,光標移動到下一行的開頭
????回車:? return,光標移動到本行開頭
????水平制表符:tab鍵,光標移動下一組4個空格開始處
????退格:backspace鍵,回退一個字符
可以使用如下轉義字符
換行: \n
回車: \r
水平制表符: \t
退格: \b
print('abc\ndef')?#?abcdef?換行顯示 print('abc\rdef')?#?def?回車,移動到光標本行開頭 print('123456\t123\t45')?#?123456??123????45?制表符4個空格,按整體字符長度算 print('abc\bdef')?#?abdef?退格,刪除了c
2、使用反斜杠"\",作為轉義符
例如想直接打印一個字符串,但想在該字符串中包含單引,雙引等,需要使用\進行轉義
print('what\'s?you?name')?#?what's?you?name print('打印一個雙引號\"')?#?打印一個雙引號" print('打印一個反斜杠\\')?#?打印一個反斜杠\
3、原始字符串
例如想打印 '\tC:\Program Files' -t為制表符,不想讓-t生效,那么可以使用到原始字符串raw
print('\t:programfiles')??# :programfiles print(r'\t:programfiles')?#\t:programfiles print(R'\t:programfiles')?#\t:programfiles
4、字符串查操作
? ?a)? 使用index、rindex、find、rfind查找字符串中元素索引
s?=?'12334567' print(s.index('3'))?#?2? print(s.rindex('3'))?#?3 print(s.find('3'))??#?2? print(s.rfind('3'))?#?3 #?可以指定查找start和stop,例如從索引1開始到5結束,查找元素3的索引 print(s.index('3',1,5))?#?2 print(s.rfind('3',1,5))?#?3 #?當查找的元素不在指定索引中時,index和rindex方法會拋出value?error #?當查找的元素不在指定索引中時,find和rfind方法會返回-1 print(s.index('9',1,5))?#?ValueError:?substring?not?found print(s.rindex('9',1,5))?#?ValueError:?substring?not?found print(s.find('9',1,5))??#?-1? print(s.rfind('9',1,5))?#?-1
?b) 亦可使用查找列表查操作,查找對應索引的元素
s?=?'Python' print(s[3])??#?h print(s[1:4])?#?yth print(s[:-1])?#?Pytho print(s[::-1])?#?nohtyP print(s[:])??#?Python print(s[:2:-1])?#?noh
5、字符串的比較
a)? 使用 >, <, ==, !=對字符串進行比較
s1?=?'abc' s2?=?'def' print(s1?==?s2)?#?False print(s1[0]?<?s2[0])?#?True print(s1[1]?>?s2[0])?#?False print(s1?!=?s2)?#?True
b)?字符串也可以適用is,==是比較相等性,is是比較同一性,但是對于字符串來說,python重復進行調用
a?=?b?=?'123' c?=?'123' print?(a?is?b)?#True print?(a?==?c)?#True print?(a?is?c)?#True print(id(a),id(c))??#139917133452656?139917133452656
6、字符串的反轉
使用內置函數reversed對字符串進行反轉
s?=?'hello?world' print(list(reversed(s)))??#?['d',?'l',?'r',?'o',?'w',?'?',?'o',?'l',?'l',?'e',?'h']
7、字符串的排序
使用內置函數sorted對字符串進行排序
s?=?'EdfaCb' #?可以指定排序規則,例如先轉換成小寫,然后進行排序,或者排序完對字符串進行反轉 #?不指定規則,則按照ord()的方式排序 print(sorted(s,key?=?str.lower))?#?['a',?'b',?'C',?'d',?'E',?'f'] print(sorted(s,reverse?=?True))?#?['f',?'d',?'b',?'a',?'E',?'C'] print(sorted(s))?#?['C',?'E',?'a',?'b',?'d',?'f']
8、字符串的大小寫轉換
a) upper 將所有字符轉換成大寫
b) lower 將所有字符轉換成小寫
c) swapcase 把所有小寫轉換成大寫,大寫轉小寫
d) title 把每個單詞的開頭轉換成大寫
s?=?'java?PytHon?Shell' print(s.lower())?#java?python?shell print(s.upper())?#?JAVA?PYTHON?SHELL print(s.swapcase())?#?JAVA?pYThON?sHELL print(s.title())?#?Java?Python?Shell
9、字符串的對齊
a) center 中心對齊
b) rjust 右對齊
c) ljust 左對齊
以上方法可以指定兩個參數,第一個參數是寬度,第二個參數是對齊符號,不指定第二個參數默認是空格。
d) zfill:右對齊,左邊用0填充
該方法值接收一個參數,用于指定字符的寬度,如果指定的字符寬度小于字符串本身,那么返回字符串本身
s?=?'hello?world' print(s.center(20,'*'))?#?****hello?world***** print(s.ljust(18,'^'))?#?hello?world^^^^^^^ print(s.rjust(18,'$'))?#?$$$$$$$hello?world print(s.zfill(15))?#?0000hello?world
10、字符串的替換
調用replace方法,對字符串進行替換,str.replace('old','new','replace_num')
s?=?'hi?hi?hi?hello' #將hi替換為hello,最大替換個數為2個,不指定替換個數默認為全部 print(s.replace('hi','hello',2))?#?hello?hello?hi?hello
11、去除字符串的前導符和后導符
調用方法lstrip、rstrip、strip對字符串前導符和后導符進行去除
s?=?'****hello?world^^^^^' print(s.lstrip('*'))??#?hello?world^^^^^ print(s.rstrip('^'))??#?****hello?world print(s.strip('*^'))??#?hello?world
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。