For length is 5 One of the bits 01 strand , Everyone can be 0 or 1, Altogether 32 Maybe . The first few of them are :
00000
00001
00010
00011
00100
Please output this in order of small to large 32 Kind of 01 strand .
Output 32 That's ok , The length of each line in the order from small to large is 5 Of 01 strand .
# Method 1 :
for i in range(0,32):
print("{0:0>5b}".format(i))
# Method 2 :
for a in range(0, 2):
for b in range(0, 2):
for c in range(0, 2):
for d in range(0, 2):
for e in range(0, 2):
print(str(a) + str(b) + str(c) + str(d) + str(e))
1. The length is 5 Of 01 String , The maximum value is 11111, convert to 10 Hexadecimal is 31, The idea of method 1 is to start with 0~32 Search once in this range , And then convert the retrieved number into binary number for output
2.format() Function by {} and : To replace the old %
The number before the colon is actually the serial number , When format When only one element needs to be output , This serial number can be omitted ( A colon cannot be omitted )
format Use >、、< To indicate left alignment 、 Align center 、 Right alignment . Align symbols in (>、、<) The number added after , Width , Adding a specific character before the alignment symbol indicates that when the output is not enough to take up the full width , Fill with specific characters .
This question is to use 0 placeholder , Right alignment , Width is 5
About formate For detailed usage of the function, please refer to the following article
Python Format output format function
2. The second way is through 5 Layer cycle + Character splicing to achieve output