Python學習筆記3-字串

來源:互聯網
上載者:User

標籤:

格式化字串/複合欄位名

>>> import humansize>>> si_suffixes = humansize.SUFFIXES[1000]>>> si_suffixes[‘KB‘, ‘MB‘, ‘GB‘, ‘TB‘, ‘PB‘, ‘EB‘, ‘ZB‘, ‘YB‘]>>> ‘1000{0[0]} = 1{0[1]}‘.format(si_suffixes)‘1000KB = 1MB‘

 

>>> import humansize>>> import sys>>> ‘1MB = 1000{0.modules[humansize].SUFFIXES[1000][0]}‘.format(sys)‘1MB = 1000KB‘

 

Sys.modules 是一個儲存當前python執行個體中搜有已匯入模組的字典。模組的名字為鍵,模組自身為值。

 

 >>> s = ‘‘‘finished files are the re-sults of years of scientific study combined with theexperience of years. ‘‘‘ >>> s.splitlines()[‘finished files are the re-‘, ‘sults of years of scientific study combined with the‘, ‘experience of years. ‘] >>> print(s.lower())finished files are the re-sults of years of scientific study combined with theexperience of years. 

 

 

 >>> a_list = query.split("&")>>> a_list[‘user=pilgrim‘, ‘database=master‘, ‘password=PapayaWhip‘] >>> a_list_of_list = [v.split(‘=‘,1) for v in a_list]>>> a_list_of_list[[‘user‘, ‘pilgrim‘], [‘database‘, ‘master‘], [‘password‘, ‘PapayaWhip‘]] >>> a_dict = dict(a_list_of_list)>>> a_dict{‘password‘: ‘PapayaWhip‘, ‘database‘: ‘master‘, ‘user‘: ‘pilgrim‘}

  

split()-根據指定的分隔字元,將字串分隔成一個字串列表。

dict() - 將包含列表的列錶轉換成字典對象

 

 

字串的分區

 

>>> a_string = "My alphabet starts where your alphabet ends.">>> a_string[3:11]‘alphabet‘ >>> a_string[3:-3]‘alphabet starts where your alphabet en’ >>> a_string[:18]‘My alphabet starts’ >>> a_string[18:]‘ where your alphabet ends.‘

 

 

String VS. Bytes

 

Bytes對象的定義:b’ ’, eg: by = b’abcd\x65’

Bytes對象不能改變其值,但可以通過內建函數bytearry()將bytes對象轉化成bytearry對象,bytearry對象的值可改變

 

 >>> by = b‘abcd\x65‘>>> barr = bytearray(by)>>> barrbytearray(b‘abcde‘) >>> barr[0]=102>>> barrbytearray(b‘fbcde‘) 

 

>>> a_string = "dive into python">>> by = a_string.encode(‘utf-8‘)>>> byb‘dive into python‘>>> roundtrip = by.decode(‘big5‘)>>> roundtrip‘dive into python‘

 

string.encode() -- 使用某種編碼方式作為參數,將字串轉化為bytes對象。

bytes.decode() -- 使用某種編碼方式作為參數,將bytes對象轉化成字串對象。

 

Python學習筆記3-字串

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.