String formatting approach to convert numbers to hexadecimal

  1. Take input from the user using the input function and convert it to an integer using the int function. This gives us the value of N.
  2. Loop through the numbers from 1 to N using the range function.
  3. Inside the loop, use string formatting to convert each number to its hexadecimal value. To do this, use the following expression: “{0:x}”.format(i). This expression formats the value of i as a lowercase hexadecimal string.
  4. Print the hexadecimal value using the print function.
  5. Here, the {0:x} part in the string formatting expression is a placeholder that represents the value of i. The 0 indicates the index of the value to be formatted (in this case, the first value in the parentheses), and the x specifies that the value should be formatted as a lowercase hexadecimal string.

Python3




# take input from user
n = 11
 
# loop through the numbers from 1 to N
for i in range(1, n+1):
    # use string formatting to convert the number to hexadecimal
    hex_value = "{0:x}".format(i)
    # print the hexadecimal value
    print(hex_value)


Output

1
2
3
4
5
6
7
8
9
a
b

The time complexity of the string formatting approach to convert numbers to their hexadecimal values is O(N),

The auxiliary space of this approach is O(1).

Python program to print the hexadecimal value of the numbers from 1 to N

Given a number N, the task is to write a Python program to print the hexadecimal value of the numbers from 1 to N.

Examples:

Input: 3
Output: 1
        2
        3

Input: 11
Output: 1
        2
        3
        4
        5
        6
        7
        8
        9
        a
        b

Approach:

  • We will take the value of N as input.
  • Then, we will run the for loop from 1 to N+1 and traverse each “i” through the hex() function.
  • Print each hexadecimal value.

Note: The hex() function is one of the built-in functions in Python3, which is used to convert an integer number into its corresponding hexadecimal form.

Below are the implementations based on the above approach:

Python3




# Python program to print the hexadecimal value of the
# numbers from 1 to N
 
# Function to find the hexadecimal value of the numbers
# in the range 1 to N
def hex_in_range(n):
     
    # For loop traversing from 1 to N (Both Inclusive)
    for i in range(1, n+1):
         
        # Printing hexadecimal value of i
        print(hex(i)[2:])
 
# Calling the function with input 3
print("Input: 3")
hex_in_range(3)
 
# Calling the function with input 11
print("Input: 11")
hex_in_range(11)


Output

Input: 3
1
2
3
Input: 11
1
2
3
4
5
6
7
8
9
a
b

Time complexity : O(N), as it performs a loop operation for every number from 1 to N, and the time taken increases linearly with the input value N.

Space complexity : O(1), as it only uses a constant amount of memory regardless of the input value N.

Similar Reads

Approach: String formatting approach to convert numbers to hexadecimal

...

Method: Manual conversion method

Take input from the user using the input function and convert it to an integer using the int function. This gives us the value of N. Loop through the numbers from 1 to N using the range function. Inside the loop, use string formatting to convert each number to its hexadecimal value. To do this, use the following expression: “{0:x}”.format(i). This expression formats the value of i as a lowercase hexadecimal string. Print the hexadecimal value using the print function. Here, the {0:x} part in the string formatting expression is a placeholder that represents the value of i. The 0 indicates the index of the value to be formatted (in this case, the first value in the parentheses), and the x specifies that the value should be formatted as a lowercase hexadecimal string....

Approach: Using bitwise operators to convert decimal numbers to hexadecimal.

...