This is a hackerrank practice problem https://www.hackerrank.com/challenges/python-string-formatting/problem . It is not actually a difficult problem, but one of the solutions posted in the discussion is confusing. I am adding their function for doing the same below:
def print_formatted(number):
p = len(f"{number:b}")
for i in range(1,number+1): print(f"{i: >{p}} {i: >{p}o} {i: >{p}X} {i: >{p}b}")
- Even though b is not defined it is not throwing an error. If I just do 5:6 in python, it throws an error. But if I do print(f"{5:6}"), it prints adds 6 spaces before 5 and prints it. What exactly is happening here and what are the common use cases for this?
- What exactly is happening in the print statement? What does all the o, X nd b do? Why is there a {} inside one active {} and what does it do?