Write a Python program to print four values decimal, octal, hexadecimal (capitalized), binary in a single line of a given integer


This program takes an integer input from the user and converts it into its equivalent values in decimal, octal, hexadecimal and binary formats.

  • The input integer is obtained from the user using the input() function, and is then converted to integer type using the int() function and assigned to the variable i.
  • The oct() function is used to convert the decimal input integer i into its corresponding octal representation. The str() function is used to convert the octal value to a string and the [2:] slice is used to remove the first two characters (which are '0o') that are added to the beginning of the octal representation by the oct() function.
  • The hex() function is used to convert the decimal input integer i into its corresponding hexadecimal representation. The str() function is used to convert the hexadecimal value to a string and the [2:] slice is used to remove the first two characters (which are '0x') that are added to the beginning of the hexadecimal representation by the hex() function. The upper() method is called on the h variable to capitalize all the letters in the hexadecimal representation.
  • The bin() function is used to convert the decimal input integer i into its corresponding binary representation. The str() function is used to convert the binary value to a string and the [2:] slice is used to remove the first two characters (which are '0b') that are added to the beginning of the binary representation by the bin() function.
  • The resulting decimal, octal, hexadecimal (capitalized), and binary values are then printed to the console using the print() function. The output is formatted using string concatenation to include the corresponding label for each converted value.

Source Code

i = int(input("Input an integer: "))
o = str(oct(i))[2:]
h = str(hex(i))[2:]
h = h.upper()
b = str(bin(i))[2:]
d = str(i)
print("Decimal : ",d)
print("Octal : ",o)
print("Hexadecimal (capitalized) :",h)
print("Binary :",b)

Output

Input an integer: 10
Decimal :  10
Octal :  12
Hexadecimal (capitalized) : A
Binary : 1010


Example Programs