Python built-in functions (1)


built-in method




demo:

def function1():
    test1 = "ABCDefghi\tabcd"
    print(test1)
    print(test1.count("a")) #Specify the number of times the character appears
    print(test1.endswith("d")) #Whether it ends with the specified character
    print(test1.expandtabs()) #Convert tab(\t) symbols to spaces, no parameters are specified, the default is 8 spaces
    print(test1.find("f")) #The first occurrence of the specified character is subscripted
    print(test1.index("e")) #Same as the find method, if the character does not exist, an error is reported
    print(test1.isalnum()) #Judging that str has at least one character, and all characters are letters or numbers
    print(test1.isalpha()) #Judging that str has at least one character, and all characters are letters
    test2 = "1234fs"
    print(test2.islower()) #The characters in str are all lowercase and can contain numbers

    print(test1.isdecimal()) #Judging that str only contains decimal numbers
    print(test1.isdigit()) #Judging that str only contains numbers
    print(test1.isnumeric()) #Judging that str contains only numeric characters

    print("------------------------")
    test3 = "afjjfkf  "

    print(test3.rjust(40)) #Right-aligned string, the length is 40, and the left is not filled with spaces
    print(test3.partition("f")) #The first f from the left starts to separate characters, left, f, right
    print(test3.rpartition("f"))
    print("-".join(test3))
    print(" ".isspace()) #Whether it is a space
    print("   jfkl".lstrip())
    print("sabdabdabd".replace('a', '0'))
    print("21,23,213,3".split(","))
    print("bbbVVVVVbhhhhV".swapcase()) #Uppercase to lowercase, lowercase to uppercase
    print("aaeeassssaara".strip('a')) #eeassssaar removes the characters at both ends of a, not written as removing spaces
    print("AhhFk".istitle()) #Determine whether it is a title, the title has only one uppercase letter, False

    print("qwerqwer".maketrans('q', 'e'))   #{113: 101}

    print("qwerqwer".upper()) #lowercase to uppercase, QWERQWER
    print("qwerqwer".zfill(15)) #0000000qwerqwer

3. String formatting


The following operators can be used in combination with the above formatting operators



4. Sequence

  1)max、min

 Both lists and tuples are acceptable. Requirements: The comparison elements are of the same data type.

    test = [13,32,2,-23,-90,34]
    tuple1 = (23,-324,4,-34,490,4)
    print(max(tuple1))   #490
    print(min(tuple1))   #-324
    print(max(test))     #34
    print (min (test)) # -90
    tuple2 = ('saf','qewr','a')
    print(max(tuple2))    #'saf'
  print(min(tuple2))    #'a'
2)sum、sorted
tuple3 = (1,32,21,9,-32)
    print(sum(tuple3))    #31
    print(sorted(tuple3))    #[-32, 1, 9, 21, 32]
    print(list(reversed(tuple3))) #[-32, 9, 21, 32, 1] reverse in place
    print(list(enumerate(tuple3))) #[(0, 1), (1, 32), (2, 21), (3, 9), (4, -32)] : (index, value) element Group form, decomposed into multiple tuples
    print(list(zip(tuple1,tuple3))) #[(23, 1), (-324, 32), (4, 21), (-34, 9), (490, -32)] : two lists One-to-one correspondence according to the following table, take the common part

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326684998&siteId=291194637