chr() in Python

Video Tutorial
FREE
ord & chr in Python thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

Overview

The chr() method in Python returns a string representing a character and a Unicode code integer. chr(98) returns the text 'b', for example. This method accepts an integer as the parameter. The chr() function in Python returns an exception if it exceeds the limit supplied. The argument's normal range goes from 0 to 1,114,111.

Syntax for chr() in Python

In python, the syntax of chr() function is written as chr(j).

Here j is an integer. As discussed, the range of the integer j should lie between the numbers 0 and 1,114,111.

Parameters for chr() in Python

Only one parameter is used in chr() in python. This parameter needs to be of the data type int. The integer has an acceptable value from 0 to the number 1,114,111.

Return Value for chr() in Python

This method is responsible for returning a character's string format.

Return Value of Chr in Python

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Exceptions for chr() in Python

ValueError exception will be generated if the integer j, passed as a parameter, is not in the limit.

Example for chr() in Python

It is a simple demonstration of how to use the chr() method in Python, which returns the character existing at the supplied int number. The function returns a string, which may also be checked.

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

What is chr() in Python?

When given an integer data type in Python, we often prefer to figure out what character it represents (basically known as the ASCII values). In Python, we have a function called chr() that accepts a number as a parameter and then returns a character. The range of arguments can be provided to its numeric parameter (0 to the number 1,114,111).

For instance, the chr value for number 68 is 'D' while the value for the number 92 is 'a'.

What is chr in Python

How to Use the chr() Function in Pythonn?

Using the chr() method to get the character-related with any ASCII format, comprising those which can't be typed or presented on a screen. You can use chr in python to check for such particular characters in fields or records.

For example, if there are various integers provided, and we want to convert them into characters to print the output.

Output:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

More Examples

Let's look at several samples of the chr() method to see how it works.

Example 1: How chr() works?

Let's go through a small snippet to know how the chr() method works in Python.

Output:

Here, the integers passed are - 98, the Unicode of the letter b, 67, the Unicode of the letter C, and 1200, the Unicode of Ұ.

Example 2: Integer passed to chr() is out of the range

The chr() function takes an integer value in a range. It throws an error if the value exceeds the range. See the example below.

Output:

We'll go through one more example to understand the working of chr in python.

Output:

ValueError is reported when the code is run. This is because the parameter given to the chr() function is out of range. As discussed above, the range lies between 0 to 1,114,111. Here, the chr(11111111) equals 1,114,112 in decimal and 0x110000 in hexadecimal.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example 3: Converting all the integers to ASCII characters using for loop

Output:

Example 4: Print characters from a given list of numbers using chr function

Output:

Conclusion

  • The chr() is most commonly used to turn an ASCII value into a character.

  • The chr() is the opposite of the ord() method. chr() in python converts integers to char data types, whereas The ord() method returns a number representing the character's Unicode code.

  • The chr() method can take a single input, which must be a number.

  • The integer's acceptable range lies from 0 to 1,1141,111(0x10FFFF in base 16), which corresponds to comparable Unicode characters.

  • Python shall throw a ValueError exception if you pass an integer data type beyond the limits discussed above.

  • The chr() function is responsible for returning a single-character string with a numeric Unicode code point.

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more