Python's basic data types and their common methods

Source: Internet
Author: User

First, the number int
    • Int.bit_length ()

used to calculate the number of bits that the number occupies in the binary form.        

A = 9  #  9 binary is 1001b = a.bit_length ()print(b)

Results:

4

Second, String str
    • Str.upper ()

Converts a lowercase letter in a string to uppercase:

" ABCDE "  = a.upper ()print(b)

The results are as follows:

ABCDE
    • Str.lower ()

lowercase letters in the string are converted:

" ABCDE "  = a.lower ()print(b)

The results are as follows:

Abcde
    • Str.replace ()

Replace the specified character or string with the following:

" ABCDEDECDE "  = a.replace ('cde'xue')Print (b)

The results are as follows:

Abxuedexue

"CDE" as the first parameter, is the replaced object, "Xue" as the second parameter, is the object that is substituted. The method also has a third parameter, which is the number of substitutions, and the default value is-1, which means replacing all matching characters (strings).

    • Str.find ()

Finds the character or string specified in the string, returns the subscript of the first matching character (string), and returns 1 if it is not found:

" ABCDEDECDE "  = a.find ('cde'= a.find ('z') Print (b) Print (c)

The results are as follows:

2-1
    • Str.strip ()

Eliminates substrings of strings and strings given in a string (no arguments are cleared for whitespace characters):

" CDABCDEDECDE "  = A.strip ('cde')print(b)

The results are as follows:

Ab

Description: The substring of ' CDE ' is: ' CDE ', ' CD ', ' De ', ' C ', ' d ', ' e ', the Strip method matches the substring of ' CDE ' to the string object and, if successful, removes the substring. The end result is AB.

    • Str.split ()

The string is sliced by the specified character (string), and the result returns a list:

" CDABCDEDECDE "  = a.split ('cd')print(b)

Results:

["'ab''Ede'E  ']
    • Str.join ()

Inserts a character (string) between the characters of the specified string:

" CDABCDEDECDE "  '_+_'. Join (a)print(b)

Results:

C_+_d_+_a_+_b_+_c_+_d_+_e_+_d_+_e_+_c_+_d_+_e

' _+_ ' is the inserted object, and the parameter of join () is the inserted object.

Iii. List of lists

Four, tuple tuple

V. Dictionary Dict VI, boolean bool

Python's basic data types and their common methods

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.