String Formatting in Python HackerRank Solution

Hello Programmers, In this post, you will know how to solve the String Formatting in Python HackerRank Solution. This problem is a part of the HackerRank Python Programming Series.

String Formatting in Python HackerRank Solution
String Formatting in Python HackerRank Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

String Formatting in Python HackerRank Solutions

problem

Given an integer , n, print the following values for each integer i from 1to n :

  1. Decimal
  2. Octal
  3. Hexadecimal (capitalized)
  4. Binary

The four values must be printed on a single line in the order specified above for each i from 1 to n. Each value should be space-padded to match the width of the binary value of n.

Input Format :

A single integer denoting n.

Constraints :

  • 1 <= n <= 99

Output Format :

Print n lines where each line i (in the range  1< i < n ) contains the respective decimal, octal, capitalized hexadecimal, and binary values of i. Each printed value must be formatted to the width of the binary value of n.

Sample Input :

17

Sample Output :

    1     1     1     1
    2     2     2    10
    3     3     3    11
    4     4     4   100
    5     5     5   101
    6     6     6   110
    7     7     7   111
    8    10     8  1000
    9    11     9  1001
   10    12     A  1010
   11    13     B  1011
   12    14     C  1100
   13    15     D  1101
   14    16     E  1110
   15    17     F  1111
   16    20    10 10000
   17    21    11 10001   

String Formatting in Python HackerRank Solutions

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

Disclaimer: The above Problem (String Formatting in Python) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Alphabet Rangoli in Python HackerRank Solution

Leave a Reply

Your email address will not be published. Required fields are marked *