Hackerrank String Formatting solution in Python YASH PAL, 31 July 20241 August 2024 In this HackerRank String Formatting problem solution in Python Given an integer, n, print the following values for each integer i from 1 to n: Decimal Octal Hexadecimal (capitalized) Binary String formatting solution HackerRank String formatting solution in Python 2 programming. N = int(raw_input()) width = len(str(bin(N)))-2 for num in range(1,N+1): for base in 'doXb': print '{0:{width}{base}}'.format(num, base=base, width=width), print Problem solution in Python 3 programming. N = int(input()) l = len(bin(N)) - 2 for i in range(1, N + 1): f = "" for c in "doXb": if f: f += " " f += "{:>" + str(l) + c + "}" print(f.format(i, i, i, i)) Problem solution in PyPy programming. n = int(raw_input()) width = len("{0:b}".format(n)) for i in xrange(1,n+1): print "{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(i, width=width) Problem solution in pypy3 programming. def print_formatted(number): width=len(bin(number))-2 for num in range(1,number+1): for base in ('d', 'o', 'X', 'b'): print("{0:{width}{base}}".format(num, base=base, width=width), end=' ') print() Note: in the above code in the for loop there is only one single line of code. for visibility purpose am going to cut down that line of code into another line. coding problems hackerrank solutions python HackerRankPython
1.First of all width must match binary number length.So after defining the function. 2.In line 2 he is assigning the variable with length of binary value by deducting base value length example 'ob1', for 'ox1' list goes on. 3.In line 4 code will check all the bases all are string formatting code, built in it will filter all required values.last line again using string format to print width, base again for better understanding you can dig more.follow thislinkhttps://docs.python.org/3/library/string.html#custom-string-formatting