BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python chr() function with examples

By Chaitanya Singh | Filed Under: Python Tutorial

The chr() function in Python accepts an integer which is a unicode code point and converts it into a string representing a character.

Python chr() function

String chr(num)

The valid range for num is 0 to 1,114,111

Returns a string representing a character whose unicode code point value is num.

Python chr() function Example

In the following example we are passing integers values in the chr() function that represents the unicode code point values and the function returns the corresponding characters (strings).

This function returns a string with length 1 which represents the character whose unicode code point value is passed in chr() function.

myString = chr(101)
print(myString)

myString2 = chr(47)
print(myString2)

myString3 = chr(1002)
print(myString3)

Output:
Python chr() function example

Python chr() function ValueError Example

As I have already mentioned that the valid range for the integers that can be passed in the chr() function is 0 to 1,114,111, if we pass the value out of this range then we get the ValueError.

# ValueError example
myString = chr(-10)
print(myString)

Output:
Python chr function value error example

Comments

  1. S k jha says

    March 27, 2020 at 2:55 AM

    In chr function if I input 111411 which is within the range, in that case why it gives error

    Reply

Leave a Reply Cancel reply

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

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap