We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
So, I love f-strings. This was the first time I've seen where something seemed a bit more hacky than with format strings, but I made it work. In particular, the start_pad bothers me, not sure why >{pad}d did not work.
pad = len(bin(number)[2:])
for i in range(1, number+1):
start_pad = ' ' * (pad - len(str(i)))
print(f"{start_pad}{i} {i:>{pad}o} {i:>{pad}X} {i:>{pad}b}")
The [2:] is to strip the '0b' off the start of the return value from bin().
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
String Formatting
You are viewing a single comment's thread. Return to all comments →
So, I love f-strings. This was the first time I've seen where something seemed a bit more hacky than with format strings, but I made it work. In particular, the start_pad bothers me, not sure why
>{pad}d
did not work.The [2:] is to strip the '0b' off the start of the return value from bin().