HackerRank- Python String Formatting




Given an integer, n

, print the following values for each integer i

from 1

to
n
:
Decimal,
Octal,
Hexadecimal (capitalized),
Binary.
The four values must be printed on a single line in the order specified above for each

from
1
to
n
. Each value should be space-padded to match the width of the binary value of n

.



  1. def print_formatted(number):
  2. l1 = len(bin(number)[2:])
  3. for i in range(1,number+1):
  4. print(str(i).rjust(l1,' '),end=" ")
  5. print(oct(i)[2:].rjust(l1,' '),end=" ")
  6. print(((hex(i)[2:]).upper()).rjust(l1,' '),end=" ")
  7. print(bin(i)[2:].rjust(l1,' '),end=" ")
  8. print("")
  9. if __name__ == '__main__':
  10. n = int(input())
  11. print_formatted(n)




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube