Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert an Integer to Character in Python

  • 11 May, 2025
  • Com 0
Converting from integer to character in Python

Python does not have a character data type. Instead, it has a string data type, and you can represent a character as a string with length 1.

When it comes to converting an integer (int) to a character (char), it involves transforming a numeric value into its corresponding character representation, typically using ASCII or Unicode mappings.

Converting an Integer to Character in Python

Python provides a built-in way called the “chr()” function to convert an integer to a character. It accepts an integer argument and returns a string representing a character at that Unicode code point.

Conversion using ASCII/Unicode

my_int = 70

print(my_int)
# Output: 70

print(type(<meta charset='utf-8'>my_int))
# Output: <class 'int'>

# Convert integer to character
my_char = chr(my_int)

print(my_char)
print(type(my_char))

print(chr(65))
# Output: A

print(chr(97))
# Output: a

List of integers to characters

What if we have a list of integers and we want to convert each element to its corresponding character type:

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

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

# 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

Error handling

If you pass an invalid code point to chr(), it will raise a ValueError.

try:
    print(chr(1114112))  # Out of range
except ValueError as e:
    print(e)
    
# Output: ValueError: chr() arg not in range(0x110000)

In the above code, you can see that we used the try/except mechanism to handle the potential ValueError, which prevents the program from crashing.

Convert an Integer to a String Character (e.g., 5 to ‘5’)

Converting int to string using str() method in Python

In this case, we will convert an integer to its string representation. For example, 5 will be converted to “5”. For that, we can use the “str()” function or “string formatting”.

Note that we are not converting to its ASCII value here.

int_value = 9
print(type(int_value))
# Output: <class 'int'>
print(str(int_value))
# Output: "9"
print(type(str(int_value)))
# Output: <class 'str'>


int_value = 123
print(type(int_value))
# Output: <class 'int'>
print(str(int_value))
# Output: "123"
print(type(str(int_value)))
# Output: <class 'str'>

The above program shows that it returns a single-character string for a single integer and a multi-character string for multi-digit integers.

Handling negative integers

Negative integers don’t directly convert to an ASCII value, but you can convert them into a string representation using the str() function.

# Handle negative integers
int_value = -72
try:
    print(chr(int_value))
    # Output: ValueError: chr() arg not in range(0x110000)
except ValueError as e:
    print(e)

# Alternative: Convert to string
print(str(int_value))
# Output: -72

We got the ValueError because the chr() function does not accept a negative integer, as Unicode code points are non-negative. For string conversion, str() includes the negative sign (e.g., -72 becomes “-72”).

Post Views: 53
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Define a Variable inside If Statement in Python
How to Print a Newline in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend