Today our in-built function is chr() which is useful for converting an ASCII value to it’s corresponding character.
Need of chr() function in Python:
Some times it requires to convert an ASCII value to it’s corsponding character and this in-built function will give python this capability.
Python chr function Syntax:
Examples: Let us start with chr() function with some examples and see how we can use them in Python coding.
Example1: Convert an ASCII value to it’s corresponding character.
Example2: Convert a string to an ASCII values.
>>>list_ascii=[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] >>> for i in list_ascii: ... print chr(i) ...
Example3: Not satisfied with the output and want to convert an ASCII to list of it’s corresponding characters? Use following code
>>>list_ascii=[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] >>> list_char=[chr(i) for i in list_ascii] >>> print list_char['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'] >>> str1=''.join(list_char) >>> print str1 Hello World
Related functions: chr(), int()
Latest posts by Surendra Anne (see all)
- FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018
- wget download a file to a directory in Linux/Unix - June 4, 2018
- GIT: How to compare two GIT branches? - June 3, 2018