python basic data type usage

number (integer) int

1. int convert string to number

1 a = "123"
2 b = int(a)
3 print(b)

  Extension 1, use type to check whether the data type is checked and converted

a = "123"
print(type(a),a)
b = int(a)
print(type(b),b)

  Extension 2, conversion between bases

num = " a " 
v = int(num , base=16 ) # Convert to decimal in decimal, currently convert from hexadecimal to decimal
 print (v)

2. Calculate the binary of the current number, represented by at least a few bits using the bit_length method

  #Calculate the binary of the current number, representing at least a few bits 
a = 10 
v = a.bit_length()
 print (v)

string str

1. Convert letter case

  #change the first letter to uppercase 
a = " Ma " 
v = a.capitalize()
 print (v)
   #change all letters to lowercase 
a2 = " MA " 
v2 = a2.casefold()
 print (v2)

2. Set the width and display it in the center. The first parameter is what to fill in the rest of the position. If it is not set, it will be blank by default.

 #Set the width and display it in the center, the first parameter is what to fill the rest of the position with, if not set to blank by default 
a3 = " alex " 
v3 = a3.center(20, " * " )   #Currently set to 20 The width of the alex is centered and filled with * 
print (v3)

3. Find the number of occurrences of a character in the string. The first parameter is the starting position, and the second parameter is the ending position. If not set, the default is to start from the beginning.

 #Find the number of occurrences of a character in the string, the first parameter is the starting position, the second parameter is the ending position, and the default is to start from the beginning if not set 
a4 = " alexalexalex " 
v4 = a4.count ( " ex " ,1,8) #Currently set to start from the first digit and end at the 8th digit 
print (v4)

4. What does it start with and what does it end with, in the verification string, the first parameter is the number to start from, and the second parameter is the number to end. If not set, the default is to start from the beginning.

#What to start with, what to end with, in the verification string, the first parameter is the number from which it starts, and the second parameter is where it ends. If it is not set, the default is to start from the beginning. 
a5 = " alex " 
v5 = a5.endswith( " a " ,1,3)    #Verify whether it ends with a, 
print (v5)    #The result is Flase 
v5 = a5.startswith( " a " ) #Verify whether 
it starts with a, print (    v5) #Result is True

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325136922&siteId=291194637
Recommended