Python學習之路3,python學習之路

來源:互聯網
上載者:User

Python學習之路3,python學習之路
本節內容:

  • 常用的字串處理。
  • 格式化輸出字串。
  • 字串的替換。
  • 字串和二進位的相互轉化。
  • 字典的操作
字串操作 

常用的字串處理

name = 'vector'print(name.capitalize()) # 首字母大寫print(name.count('e')) # 判斷字元e在字串中有多少個print(name.center(50,'-')) # 一共列印50個字元,將vector放在中間,兩邊用-填充print(name.endswith('r')) # 判斷字串以什麼結尾, 如果是以r結尾則返回trueprint(name.expandtabs(tabsize=10)) # 如果在字串中加入\t,則在該位置上加入tabsize個空格print(name.find('ec')) # 找到e在字串中的位置print(name.isalnum()) # 如果字串裡面只包含英文和數字, 則返回Trueprint(name.isalpha()) # 如果字串裡面是純英文, 則返回Trueprint(name.isdecimal()) # 如果字串是十進位數字, 則返回Trueprint(name.isdigit()) # 如果字串是否為整數, 則返回Trueprint(name.isidentifier()) # 判斷是否是一個合法的標識符(變數名), 合法則返回Trueprint(name.islower()) # 判斷是否為小寫, 是則返回Trueprint(name.isupper()) # 判斷是否為大寫, 是則返回Trueprint(name.isspace()) # 判斷是否為空白格, 是則返回Trueprint('My Name Is'.istitle()) # 判斷是否為標題(首字元大寫), 是則返回Trueprint('+'.join(['1','2','3'])) # 將列表裡面的每個元素用'+'拼接起來print(name.ljust(50,"*")) # 如果字串長度小於50, 則不夠的在左側用*補充print(name.rjust(50,"*")) # 如果字串長度小於50, 則不夠的在右側用*補充print(name.lower()) # 將大寫變成小寫print(name.upper()) # 將小寫變成大寫print('\nvector'.lstrip()) # 去掉字串左邊的空格和斷行符號print('vector\n'.rstrip()) # 去掉字串右邊的空格和斷行符號print('\nvector\n'.strip()) # 去掉字串兩邊的空格和斷行符號print('name is a book'.rfind('a')) # 從右邊開始找a,返回找到的位置print('name is a book'.split(' ')) # 將字串按照空格為界分裝成列表, 不寫參數則分隔字元預設為空白格print('name is \na book'.splitlines()) # 將字串根據分行符號為界分裝成列表print('name is a book'.swapcase()) # 大小寫互換print('name is a book'.title()) # 將字串變成標題(首字母大寫)print('name'.zfill(50)) # 如果字串不夠50個,則在左邊用0填充

格式化輸出字串

name = 'vector {name} {age}'print(name.format(name = 'rev',age=123)) # 格式化字串print(name.format_map({'name':'rev','age':123})) # 字典的形式格式化字串

字串替換

#先用maketrans設定替換規則, 然後用translate執行替換p = str.maketrans('asdfgn','123456') #第一個參數是被替換的字元,第二個參數是替換的字元print('name'.translate(p)) # 將規則p傳入,列印出來的就是61me,因為n和a被6和1替換掉了print('name'.replace('n','b',1)) # 將n換成b,第三個參數表示替換幾個,如果不寫則預設全部替換

字串和二進位的相互轉化

msg = '我愛大延邊日不落帝國'print(msg) # 字串輸出print(msg.encode(encoding='utf-8')) # 將字串轉換成二進位print(msg.encode(encoding='utf-8').decode(encoding='utf-8')) # 將二進位轉換成字串
encode參數表示之前是什麼格式的,decode參數表示要轉換成什麼格式的。如果encode和decode不寫參數,預設就是utf-8。

 

字典

字典一種key - value 的資料類型,使用就像我們上學用的字典,通過筆劃、字母來查對應頁的詳細內容。

字典的特性:

  • dict是無序的
  • key必須是唯一的,so 天生去重

文法:

info = {    'str1':'zhangsan',    'str2':'lisi',    'str3':'wangwu',    'str4':'zhaoliu'}

 

常見的字典操作:

info = {    'str1':'zhangsan',    'str2':'lisi',    'str3':'wangwu',    'str4':'zhaoliu'}print(info) # 列印全部字典print(info['str2']) # 根據鍵名列印字典元素info['str1'] = 'yangzirui' # 這樣就會修改值, 如果鍵名不存在則會直接建立del info['str2'] # 刪除該元素info.pop('str2') # 這也是刪除print(info.popitem()) # 隨機刪除一個, 並返回他的索引值對print(info['str1']) # 列印資料, 如果鍵名不存在, 就會報錯,不推薦用print(info.get('str2')) # 列印資料, 如果鍵名不存在, 則返回none, 推薦print('str1' in info) # 判斷鍵名是否存在,存在列印true,否則列印falseaaa = info.setdefault('str5','liangsohohfefho') # 如果info裡面有鍵為str5的,則直接返回對應的值;如果沒有,則建立後返回對應的值a = {    'str1':'asdfsdfd',    1:2,    2:3}info.update(a) # 將字典a合并到info中,如果有交叉,則用a字典中的值info.items() # 將字典轉成列表, 列表中的每個元素都由元組組成,每個元組又原字典的每個索引值對組成c = dict.fromkeys([1,2,3],'test') # 建立一個新字典, 鍵為1,2,3, 值均為test, 如果不寫值,則均為none;後面賦的值是三個鍵共有的, 所以改一個就全都改了

 

迴圈字典:

info = {    'str1':'zhangsan',    'str2':'lisi',    'str3':'wangwu',    'str4':'zhaoliu'}#第一種迴圈方法for i in info:    print(i,info[i])# 這應該是最基本的迴圈了# 第二種迴圈方法for k,v in info.items():    print(k,v)# 這種迴圈方式的效率沒有第一種迴圈效率高,因為這種需要將字典轉換成列表

輸出結果:(兩種迴圈方式均是這樣)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.