- How to Convert Int to Char in Python
- Syntax
- Parameters
- Return Value
- Example 1: How to Use chr() method
- Example 2: Integer passed to chr() is out of the range
- Example 3: Convert a list of integers to characters
- Convert Integer to Char in Python
- How to convert integer to char in Python?
- Examples
- Get the character represented by a number in Unicode
- What if you pass a negative integer to the above function?
- Author
- Privacy Overview
- How to convert an integer to a character in Python?
- Annual Membership
- Training for a Team
- Convert a Character to an Integer and Vice Versa in Python
- Use chr() to Convert an Integer to a Character in Python
- Use ord() to Convert a Character to an Integer in Python
- Related Article — Python Character
- Related Article — Python Integer
- Convert a Character to an Integer and Vice Versa in Python
- Use chr() to Convert an Integer to a Character in Python
- Use ord() to Convert a Character to an Integer in Python
- Related Article — Python Character
- Related Article — Python Integer
How to Convert Int to Char in Python
To convert int to char in Python, you can use the “chr()” method. For example, the “ chr(97)” expression returns “a”. The chr() method returns a character (a string) from an integer (it represents the Unicode code point of the character).
Syntax
Parameters
i: 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 1: How to Use chr() method
Let’s use the chr() function to convert int to a character.
print(chr(97)) print(chr(65)) print(chr(1100))
And we get the characters in the output concerning its ASCII value. For example, the chr() method returns a character whose Unicode point is num, an integer.
Example 2: 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).
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)
We have passed an integer outside the range, and then the method returns a ValueError.
Example 3: Convert a list of integers to characters
To create a list in Python, you can 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)
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 Integer to Char in Python
In this tutorial, we will look at how to convert an integer to a char in Python with the help of some examples. We’ll be looking at use-cases of finding the char (string) representing a character whose Unicode point is the integer.
How to convert integer to char in Python?
You can use the Python built-in chr() function to get the character represented by a particular integer in Unicode. The following is the syntax –
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
Introductory ⭐
Intermediate ⭐⭐⭐
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Pass the integer as an argument. The valid range for the integer argument is from 0 through 1,114,111 (0x10FFFF in base 16). If you pass an integer outside of this range, it will result in a ValueError .
Examples
Let’s look at some examples of using the above syntax –
Get the character represented by a number in Unicode
Let’s pass the integer 65 to the chr() function.
# integer to char ch = chr(65) # display char and its type print(ch) print(type(ch))
We get ‘A’ from the function. The number 65 points to the character ‘A’ in Unicode.
Let’s look at another example.
# integer to char ch = chr(44) # display char and its type print(ch) print(type(ch))
We get the comma character, ‘,’ for the integer 44.
You can use the Python built-in ord() function to get the corresponding integer for a character in Unicode.
# char to integer print(ord('A')) print(ord(','))
We get 65 for the character ‘A’ and 26 for the character ‘$’.
What if you pass a negative integer to the above function?
Let’s pass a negative integer to the chr() function as an argument.
# integer to char ch = chr(-65) # display char and its type print(ch) print(type(ch))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [20], in 1 # integer to char ----> 2 ch = chr(-65) 3 # display char and its type 4 print(ch) ValueError: chr() arg not in range(0x110000)
We get a ValueError . This is because the passed integer is outside the valid range of 0 through 1,114,111.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Author
Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts
Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.
This website uses cookies to improve your experience. We’ll assume you’re okay with this, but you can opt-out if you wish.
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
How to convert an integer to a character in Python?
Python’s built-in function chr() returns a sunicode character equivalent of an integer between 0 to 0x10ffff.
>>> chr(a) 'd' >>> chr(300) 'Ĭ' >>> chr(65) 'A'
- Related Articles
- How to convert an integer to a unicode character in Python?
- How to convert a single character to its integer value in Python?
- How to convert an integer to a hexadecimal string in Python?
- How to convert an integer to a octal string in Python?
- How to convert an integer to an ASCII value in Python?
- How to convert an integer into a date object in Python?
- How to convert float to integer in Python?
- Convert Tuple to integer in Python
- How to convert an object array to an integer array in Java?
- How to convert a string of any base to an integer in JavaScript?
- Convert an integer to a hex string in C++
- How to convert a string vector into an integer vector in R?
- How do I convert an integer to binary in JavaScript?
- How to convert a Boolean to Integer in JavaScript?
- Program to convert roman numeral to integer in Python?
Annual Membership
Enjoy unlimited access on 5500+ Hand Picked Quality Video Courses
Training for a Team
Affordable solution to train a team and make them project ready.
- About us
- Refund Policy
- Terms of use
- Privacy Policy
- FAQ’s
- Contact
Copyright © Tutorials Point (India) Private Limited. All Rights Reserved.
We make use of First and third party cookies to improve our user experience. By using this website, you agree with our Cookies Policy. Agree Learn more
Convert a Character to an Integer and Vice Versa in Python
- Use chr() to Convert an Integer to a Character in Python
- Use ord() to Convert a Character to an Integer in Python
This tutorial discusses methods to convert a character to an integer and an integer to a character in Python.
Use chr() to Convert an Integer to a Character in Python
We can use the built-in function chr() to convert an integer to its character representation in Python. The below example illustrates this.
val = 97 chr_val = chr(val) print(chr_val)
It will result in an error if you provide an invalid integer value to this. For example:
val = 1231232323 chr_val = chr(val) print(chr_val)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) ipython-input-42-f76a9ed55c90> in module> 1 val = 1231232323 ----> 2 chr(val) ValueError: chr() arg not in range(0x110000)
Therefore, it is always good to put this code in a try. except block to catch the error, if any, and avoid any crash. The below example illustrates this:
val = 1231232323 try: chr_val = chr(val) print(chr_val) except Exception as e: print('Error:', e)
Error: chr() arg not in range(0x110000)
Use ord() to Convert a Character to an Integer in Python
We can use the built-in function ord() to convert a character to an integer in Python. The below example illustrates this.
val = 'a' try: int_val = ord(val) print(int_val) except Exception as e: print('Error:', e)
The above method also catches any invalid input and prints out the error instead of crashing the code. For example:
val = 'aa' try: int_val = ord(val) print(int_val) except Exception as e: print('Error:', e)
Error: ord() expected a character, but string of length 2 found
Related Article — Python Character
Related Article — Python Integer
Copyright © 2023. All right reserved
Convert a Character to an Integer and Vice Versa in Python
- Use chr() to Convert an Integer to a Character in Python
- Use ord() to Convert a Character to an Integer in Python
This tutorial discusses methods to convert a character to an integer and an integer to a character in Python.
Use chr() to Convert an Integer to a Character in Python
We can use the built-in function chr() to convert an integer to its character representation in Python. The below example illustrates this.
val = 97 chr_val = chr(val) print(chr_val)
It will result in an error if you provide an invalid integer value to this. For example:
val = 1231232323 chr_val = chr(val) print(chr_val)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) ipython-input-42-f76a9ed55c90> in module> 1 val = 1231232323 ----> 2 chr(val) ValueError: chr() arg not in range(0x110000)
Therefore, it is always good to put this code in a try. except block to catch the error, if any, and avoid any crash. The below example illustrates this:
val = 1231232323 try: chr_val = chr(val) print(chr_val) except Exception as e: print('Error:', e)
Error: chr() arg not in range(0x110000)
Use ord() to Convert a Character to an Integer in Python
We can use the built-in function ord() to convert a character to an integer in Python. The below example illustrates this.
val = 'a' try: int_val = ord(val) print(int_val) except Exception as e: print('Error:', e)
The above method also catches any invalid input and prints out the error instead of crashing the code. For example:
val = 'aa' try: int_val = ord(val) print(int_val) except Exception as e: print('Error:', e)
Error: ord() expected a character, but string of length 2 found
Related Article — Python Character
Related Article — Python Integer
Copyright © 2023. All right reserved