Python Ascii To Char & Python Ascii To String Examples

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.

  1. In Python, you can convert an ASCII code to its corresponding character using the built-in chr() function.
  2. The chr() function takes an integer that represents an ASCII code and returns the corresponding character.
  3. Here is the syntax for the chr() function:
    chr(ascii_code)
  4. The ascii_code argument is an integer that represents an ASCII code.

1.2 Python Ascii To Char Example.

1.2.1 Example1.

  1. 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
  2. In the above example, the integer 65 represents the ASCII code for the uppercase letter ‘A’. The chr() function is used to convert the ASCII code to its corresponding character, which is then stored in the variable char and printed to the console.

1.2.2 Example2.

  1. 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
  2. In the above example, the list ascii_codes contains three integers that represent the ASCII codes for the uppercase letters ‘A’, ‘B’, and ‘C’. The for loop iterates over each code, uses the chr() 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.

  1. In Python, you can print all ASCII characters using a loop and the built-in chr() function.
  2. 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 the chr() function.
  3. 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 { | } ~ ⌂ 
    >>>
  4. In the above example, the for loop iterates over the integers from 0 to 127, and the chr() function is used to convert each integer to its corresponding ASCII character.
  5. The end argument of the print() 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.

  1. You can also store the ASCII characters in a string or a list using a list comprehension.
  2. 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 { | } ~ ⌂
    >>>
  3. 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.
  4. 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.

  1. In Python, you can convert a string of ASCII codes to its corresponding string using the built-in chr() function and the join() method.
  2. The chr() function takes an integer that represents an ASCII code and returns the corresponding character.
  3. The join() method concatenates a sequence of strings with a specified separator.

3.1 Example1.

  1. Here is an example of using the chr() function and the join() 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!
  2. In the above example, the list ascii_codes contains integers that represent the ASCII codes for each character in the string “Hello, World!”.
  3. The join() method is used to concatenate the characters returned by the chr() function for each code in the list.
  4. The resulting string is stored in the variable string and printed to the console.

3.2 Example2.

  1. 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.
  2. 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!
  3. In this example, the string ascii_string contains ASCII codes separated by spaces.
  4. The split() method is used to split the string into a list of strings, which are then converted to integers using a list comprehension.
  5. The resulting list of integers is used to generate the corresponding string using the chr() function and the join() method, as in the previous example.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.