How will you convert an integer to a character in python?

How will you convert an integer to a character in python?

In Python, a char can either be defined as an ASCII value or, if the char carries digits only, as the number it represents. In ASCII, a particular numerical value is given to different characters and symbols for computers to store and manipulate.

To find an ASCII value of a character, use the ord() function. Let’s see how to convert ASCII value to character value.

To convert int to char in Python, use the chr() method. The chr() is a built-in Python method that returns a character (a string) from an integer (it represents the Unicode code point of the character).

Syntax

chr(i)

Arguments

The chr() method takes a single parameter which is an integer.

Return Value

The chr() function returns a character (a string) whose Unicode code point is the integer.

Example

Let’s use the chr() function to convert int to a character.

print(chr(97))

print(chr(65))

print(chr(1100))

Output

a
A
ь

And we get the characters the output concerning its ASCII value. The chr() method returns a character whose Unicode point is num, an integer.

Integer passed to chr() is out of the range.

If we pass the negative value to the chr() function then it returns ValueError: chr() arg not in range(0x110000).

print(chr(-11))

Output

Traceback (most recent call last):
  File "/Users/krunal/Desktop/code/pyt/database/app.py", line 7, in 
    print(chr(-1))
ValueError: chr() arg not in range(0x110000)

An integer we have passed is outside the range, and then the method returns a ValueError.

Convert a list of integers to characters

To create a list in Python, use the [ ] and add the values inside it.

listA = [69, 72, 78, 81, 90, 99]

for number in listA:
    char = chr(number)
    print("Character of ASCII value", number, "is ", char)

Output

Character of ASCII value 69 is  E
Character of ASCII value 72 is  H
Character of ASCII value 78 is  N
Character of ASCII value 81 is  Q
Character of ASCII value 90 is  Z
Character of ASCII value 99 is  c

Convert Python char to int

To convert char to int in Python, use the ord() method. The ord() is a built-in Python function that returns a number representing the Unicode code of a specified character.

print(ord('K'))
print(ord('B'))
print(ord('#'))
print(ord('~'))

Output

75
66
35
126

Conclusion

To convert int to character, use the chr() function. To convert integer to character, use the ord() function.

That is it for this tutorial.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string. But use of the str() is not the only way to do so. This type of conversion can also be done using the “%s” keyword, the .formatfunction or using f-stringfunction.

    Below is the list of possible ways to convert an integer to string in python:

    1. Using str() function 

    Syntax: str(integer_value)

    Example:  

    Python3

    num = 10

    print("Type of variable before convertion : ", type(num)) 

    converted_num = str(num)

    print("Type After convertion : ",type(converted_num))

    Output:

    Type of variable before convertion :  
    Type After convertion :  

    2. Using “%s” keyword

    Syntax: “%s” % integer

    Example: 

    Python3

    num = 10

    print("Type of variable before convertion : ", type(num)) 

    converted_num = "% s" % num

    print("Type after convertion : ", type(converted_num))

    Output:

    Type of variable before convertion :  
    Type after convertion :  

    3. Using .format() function

    Syntax: ‘{}’.format(integer)

    Example: 

    Python3

    num = 10

    print("Type before convertion : ", type(num)) 

    converted_num = "{}".format(num)

    print("Type after convertion :",type(converted_num))

    How will you convert an integer to a character in python?

    Output:

    Type before convertion :  
    Type after convertion : 

    4. Using f-string

    Syntax: f'{integer}’

    Example: 

    Python3

    num = 10

    print("Type before convertion : ",type(num)) 

    converted_num = f'{num}'

    print("Type after convetion : ", type(converted_num))

    Output:

    Type before convertion :  
    Type after convetion :  

                                                                                                                        5. Using __str__() method 

    Syntax:  Integer.__str__()

    Python3

    num = 10

    print("Type before convertion : ",type(num)) 

    converted_num = num.__str__()

    print("Type after convetion : ", type(converted_num))

    Output:

    Type before convertion :  
    Type after convetion :  

    How do you convert integers to characters?

    Example 2: int to char by using forDigit() We can also use the forDigit() method of the Character class to convert the int type variable into char type. Notice the expression, char a = Character. forDigit(num1, 10);

    How will you convert an integer to an Unicode character in Python?

    Python library's chr() function converts Unicode character associated to any interger which is between 0 an 0x10ffff.

    How do you get a character from a number in Python?

    chr () is a built-in function in Python that is used to convert the ASCII code into its corresponding character. The parameter passed in the function is a numeric, integer type value. The function returns a character for which the parameter is the ASCII code.

    How do you convert int to text in Python?

    In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string. But use of the str() is not the only way to do so. This type of conversion can also be done using the “%s” keyword, the .