can anyone help me to explain pyhton syntax
Question:
def print_formatted(number):
# your code goes here
padding = len(bin(number)[2:])
for i in range(1, number+1):
print(f"{i: >{padding}} {oct(i)[2:]: >{padding}} "
f"{hex(i)[2:].upper(): >{padding}} {bin(i)[2:]: >{padding}}")
I’m trying to debug above code i know the output and working of the code but i couldn’t understand is that what
": >"
doing in f string formatting
can anyone help me to explain ": > " this syntax
Answers:
It is used for padding:
print(f"{8:<3}")
8 # note three spaces
# make them visible:
print(f"{8:.<3}")
..8
In the last example the .
is used instead of spaces.
def print_formatted(number):
# your code goes here
padding = len(bin(number)[2:])
for i in range(1, number+1):
print(f"{i: >{padding}} {oct(i)[2:]: >{padding}} "
f"{hex(i)[2:].upper(): >{padding}} {bin(i)[2:]: >{padding}}")
I’m trying to debug above code i know the output and working of the code but i couldn’t understand is that what
": >"
doing in f string formatting
can anyone help me to explain ": > " this syntax
It is used for padding:
print(f"{8:<3}")
8 # note three spaces
# make them visible:
print(f"{8:.<3}")
..8
In the last example the .
is used instead of spaces.