Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

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
HackerRank string formatting solution in python
String formatting solution

Topics we are covering

Toggle
  • HackerRank String formatting solution in Python 2 programming.
  • Problem solution in Python 3 programming.
    • Problem solution in PyPy programming.
    • Problem solution in pypy3 programming.

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 solutions Hackerrank Problems Solutions Python Solutions HackerRankPython

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes