Python cast to char

How to Convert ASCII to Char in Python

In this post, we will discuss how to convert ASCII to char in Python. Based on this program we will also develop a Python program to print all alphabet from ASCII value.

ASCII stands for American Standard Code for Information Interchange. It was developed by the ANSI (American National Standards Institute) and it is used to interchange the information from a high-level language to low-level language. Machine or Computer understand only binary languages. So, the character data type represents integers. For example, the ASCII value of the letter ‘A’ is 65.

Python Program to Convert ASCII to Char

We are using the chr() function to convert the ASCII value to the character. Which is a built-in function in Python that accepts a specified Unicode (ASCII value) as an argument and returns the character.

The syntax of chr() is:

Where num is an integer value.

chr() Parameters:

chr() method takes a single parameter, an integer i. The valid range of the integer is from 0 through 1,114,111.

Return value from chr():

The chr() method returns a character whose Unicode point is num, an integer. If an integer is passed that is outside the range then the method returns a ValueError.

# Python program to convert ASCII to character # take input num = int(input("Enter ASCII value: ")) # printing character print("Character =", chr(num))

Output for the different input values:-

Enter ASCII value: 64
Character = @

Enter ASCII value: 69
Character = E

Enter ASCII value: 95
Character = _

Enter ASCII value: 107
Character = k

How to Print Alphabet From ASCII in Python

In the previous program, we will convert ASCII to a character but in this program, we will discuss how to print all alphabet from ASCII value (upper case and lower case). We are using map(), chr() and range() function to print all alphabet. The map() is a built-in function that applies a function on all the items of an iterator given as input.

# Python program to print all alphabet from ascii value # using map() and char() function list_upper = list(map(chr, range(65, 91))) list_lower = list(map(chr, range(97, 123))) # print uppercase alphabets print('Uppercase Alphabets: ', list_upper) # print lowercase alphabets print('Lowercase Alphabets: ', list_lower)

Uppercase Alphabets: [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’]Lowercase Alphabets: [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’]

Читайте также:  Ширина макета

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Источник

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?

integer to unicode 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'

Jayashree

  • 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.

Tutorials PointTutorials Point

  • 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

Источник

Оцените статью