Python numbers and strings used functions

Digital commonly used functions

  • int (arg: str) global function, the argument string into a digital, the digital return
  • int (arg1: str, arg2: base = int) global function, the argument string into a binary number and returns this number
  • bit_length (self: int) digital right class member function, the digital representation used when the minimum number of digits in binary, the number of bits returned
jek="890"
john=int(jek)
john1=int(jek,base=2)
print(john1)
print(john)
bit_num=john.bit_length()
print(bit_num)

String commonly used functions

  • len (arg: str) global function returns the length of the string argument
  • capitalize (self: str) string class member function, the string can be used when the full letter, return capitalize the first letter of the string rest lowercase letters
  • : (str self) lower the string string class member function, the whole string of letters available, returns all lowercase letters
  • count (self: str, arg1: str, arg2: int, arg3: int) within the front opening and closing): String class member function, a range of the parameter string in string ([arg2, arg3 interval is expressed as) The number of occurrences
  • endswith (self: str, arg1: str, arg2: int, arg3: int) string class member function, the parameter string is a string of the range (range expressed as: [arg2, arg3) before and after the opening and closing) of the end
  • startswith (self: str, arg1: str, arg2: int, arg3: int) is similar to the previous example
  • find (self: str, arg: str) -1 parameter string at the position of the standard character string, when not found
  • index (self: str, arg: str) parameter string at index position of the string, the program could not find the error
  • format (self: str, arg1 = arg1 ', arg2 = arg2', ...) the string class member functions, format string, the result of the call returns the formatted format detailed in

str="aleX"
str1=str.capitalize()
print(str1)
str2=str.lower()
print(str2)
s=str.count("le",1,3)
print(s)
s1=str1.endswith("x",0,4)
print(s1)
s2=str1.startswith("Al",0,3)
print(s2)
s3=str.find("l")
print(s3)
s4=str.index("l",0,2)
print(s4)

test="i am {tom},you are {liming}"
print(test)
test1=test.format(tom="11",liming="22")
print(test1)

test3="i am {0},you are {1}"
#{}内为顺序数字时,调用函数可简写
test4=test3.format("wwt","wb")
print(test4)

#另外一种格式化方式:
ls="%s is a boss"%"wwt"
num="%d"%100
s="%s:%d"%("ss",89)
print(s)
Published 13 original articles · 18 won praise · views 817

Guess you like

Origin blog.csdn.net/qzonelaji/article/details/104003117