In this article, I will tell you how to convert python ascii code to char or string with examples
1. Python Ascii To Char.
1.1 Python chr() Function Definition.
- In Python, you can convert an ASCII code to its corresponding character using the built-in
chr()
function. - The
chr()
function takes an integer that represents an ASCII code and returns the corresponding character. - Here is the syntax for the
chr()
function:chr(ascii_code)
- The
ascii_code
argument is an integer that represents an ASCII code.
1.2 Python Ascii To Char Example.
1.2.1 Example1.
- Here is an example of using the
chr()
function to convert an ASCII code to its corresponding character.>>> ascii_code = 65 >>> >>> char = chr(ascii_code) >>> >>> print(char) A
- In the above example, the integer
65
represents the ASCII code for the uppercase letter ‘A’. Thechr()
function is used to convert the ASCII code to its corresponding character, which is then stored in the variablechar
and printed to the console.
1.2.2 Example2.
- You can also convert multiple ASCII codes to their corresponding characters using a loop or a list comprehension. Here is an example using a loop.
>>> ascii_codes = [65, 66, 67] >>> for code in ascii_codes: ... char = chr(code) ... print(char) ... A B C
- In the above example, the list
ascii_codes
contains three integers that represent the ASCII codes for the uppercase letters ‘A’, ‘B’, and ‘C’. Thefor
loop iterates over each code, uses thechr()
function to convert it to its corresponding character, and prints the character to the console.
2. Python Print All Ascii Characters Example.
2.1 Example1.
- In Python, you can print all ASCII characters using a loop and the built-in
chr()
function. - ASCII characters range from 0 to 127, so you can use a
for
loop to iterate over the integers from 0 to 127 and print the corresponding characters using thechr()
function. - Here is an example of using a loop to print all ASCII characters.
>>> for i in range(128): ... print(chr(i), end=" ") ... ☺ ☻ ♥ ♦ ♣ ♠ ♫ ☼ ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ 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 [ \ ] ^ _ ` 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 { | } ~ ⌂ >>>
- In the above example, the
for
loop iterates over the integers from 0 to 127, and thechr()
function is used to convert each integer to its corresponding ASCII character. - The
end
argument of theprint()
function is set to a space character to separate each character in the output. The resulting characters are printed to the console.
2.2 Example2.
- You can also store the ASCII characters in a string or a list using a list comprehension.
- Here is an example using a list comprehension.
>>> ascii_chars = [chr(i) for i in range(128)] >>> >>> print("".join(ascii_chars)) ☺☻♥♦ ♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ >>> >>> print(" ".join(ascii_chars)) ☺ ☻ ♥ ♦ ♣ ♠ ♫ ☼ ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ 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 [ \ ] ^ _ ` 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 { | } ~ ⌂ >>>
- In this example, the list comprehension generates a list of ASCII characters by iterating over the integers from 0 to 127 and using the
chr()
function to convert each integer to its corresponding ASCII character. - The resulting list of characters is concatenated into a string using the
join()
method and printed to the console.
3. Python Ascii To String Examples.
- In Python, you can convert a string of ASCII codes to its corresponding string using the built-in
chr()
function and thejoin()
method. - The
chr()
function takes an integer that represents an ASCII code and returns the corresponding character. - The
join()
method concatenates a sequence of strings with a specified separator.
3.1 Example1.
- Here is an example of using the
chr()
function and thejoin()
method to convert a string of ASCII codes to its corresponding string.>>> ascii_codes = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33] >>> >>> string = "".join(chr(code) for code in ascii_codes) >>> >>> print(string) Hello, World!
- In the above example, the list
ascii_codes
contains integers that represent the ASCII codes for each character in the string “Hello, World!”. - The
join()
method is used to concatenate the characters returned by thechr()
function for each code in the list. - The resulting string is stored in the variable
string
and printed to the console.
3.2 Example2.
- You can also convert a string of ASCII codes separated by a delimiter to its corresponding string using the
split()
method and a list comprehension. - Here is an example source code.
>>> ascii_string = "72 101 108 108 111 44 32 87 111 114 108 100 33" >>> >>> ascii_codes = [int(code) for code in ascii_string.split()] >>> >>> string = "".join(chr(code) for code in ascii_codes) >>> >>> print(string) Hello, World!
- In this example, the string
ascii_string
contains ASCII codes separated by spaces. - The
split()
method is used to split the string into a list of strings, which are then converted to integers using a list comprehension. - The resulting list of integers is used to generate the corresponding string using the
chr()
function and thejoin()
method, as in the previous example.