Python Learning Note 3-string

Source: Internet
Author: User

format string/compound field name

>>> 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 is a dictionary that holds imported modules in the current Python instance. The module's name is the key, and the module itself is a value.

>>> 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 ()-separates the string into a list of strings, based on the specified delimiter.

Dict ()-Converts the list of included lists into a Dictionary object

Shard of a string

>>> 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

Definition of bytes object: B ', eg:by = B ' abcd\x65 '

The bytes object cannot change its value, but the Bytes object can be converted to a Bytearry object through the built-in function Bytearry (), and the value of the Bytearry object can be changed

>>> 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 ()--Converts a string to a bytes object using some encoding as an argument.

Bytes.decode ()--Converts the Bytes object to a string object using some encoding as a parameter.

Python Learning Note 3-string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.