format method and int method in python

1. Background

We need to use some other hexadecimal when performing computer hexadecimal conversion. The most common ones are binary, octal, and hexadecimal. Two methods are introduced here to complete the conversion between bases.

Second, use

2.1 format method

The format method contains two parameters, the first is a decimal number, and the second parameter indicates the formatting method. The content of the second parameter is as follows

# 0 filled characters (can only be 0) 
# 4 represents the number of digits after filling (12 means 12 digits after filling) 
# x represents the hexadecimal number (b: binary, o: octal, x: hexadecimal) , D: decimal)

The output is as follows:

print (format (22, ' 04x ' ))
 >>> 0016
 print (format (22, ' 4x ' ))   # There will be two spaces in front 
>>> 16 print (format (22, ' x ' ))
 >>> 16

2.2 int method

The int method contains two parameters, the first is the object to be int, which is generally a number in the form of characters, and the second parameter is the cardinality of conversion. The parameter form and content are as follows

# "1111", indicating a hexadecimal number, must meet the hexadecimal requirements, that is, all numbers in the ternary number are less than three 
# base, indicating a hexadecimal number, specified by a specific number, and the range is 2 <= base <= 36

The output is as follows:

print(int("11112", base=36))
>>> 1727606
print(int("1111", base=2))
>>> 15

3. Summary

The above content uses built-in functions, of course, you can also use character segmentation to do it, record it here, you can view it when it is not commonly used when needed.

Guess you like

Origin www.cnblogs.com/future-dream/p/12683780.html