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.
- Prepare
- Python
- Strings
- String Formatting
- Discussions
String Formatting
String Formatting
Sort by
recency
|
1759 Discussions
|
Please Login in order to post a comment
I thought it simplest to just calculate the length of the binary representation.
Note how
{0:specifies the first positional argument, and {1} specifies the second for width.def print_formatted(number): # your code goes here width = len(bin(number)[2:]) for i in range(1, n+1): print(str(i).rjust(width), end =' ') print(oct(i)[2:].rjust(width), end =' ') print(hex(i)[2:].rjust(width).upper(), end =' ') print(bin(i)[2:].rjust(width), end =' ') print()
if name == 'main': n = int(input()) print_formatted(n)
Here is String formatting problem solution in python 2 and python 3 -https://programmingoneonone.com/hackerrank-string-formatting-solution-in-python.html
Determine max width (binary of n) → Loop through numbers → Convert to all bases → Align right → Print in one row.
def print_formatted(number):