Sling Academy
Home/Python/Python: How to Convert a String to Character Codes

Python: How to Convert a String to Character Codes

Last updated: June 01, 2023

Char codes, or character codes, are numbers that represent graphical characters, such as letters, digits, symbols, and punctuation marks. They allow characters to be stored, transmitted, and manipulated by digital computers. For instance, in the ASCII encoding, the character “A” has the char code 65, and the character “!” has the char code 33.

Converting a string into character codes can be useful in encryption algorithms, text processing, etc. In Python, you can turn a given string into char codes by following the steps below:

  1. iterate over each character in the string using a loop.
  2. Apply the ord() function to each character to get its corresponding character code.
  3. Store the character codes in a list or any other data structure.

Code example:

def convert_to_char_codes(string):
    char_codes = []
    for char in string:
        char_codes.append(ord(char))
    return char_codes


print(convert_to_char_codes("Sling Academy"))

Output:

[83, 108, 105, 110, 103, 32, 65, 99, 97, 100, 101, 109, 121]

Instead of defining a reusable custom function, you can also write the code in the one-line style like this:

text = "Welcome to Sling Academy"

# Convert text to char codes with a single line of code
char_codes = [ord(char) for char in text]

print(char_codes)

Output:

[87, 101, 108, 99, 111, 109, 101, 32, 116, 111, 32, 83, 108, 105, 110, 103, 32, 65, 99, 97, 100, 101, 109, 121]

It is possible to make the code even shorter with the help of the map() method, like this:

text = "There are bad devils and good devils."

# Convert text to char codes
char_codes = [list(map(ord, text))]

print(char_codes)

Output:

[[84, 104, 101, 114, 101, 32, 97, 114, 101, 32, 98, 97, 100, 32, 100, 101, 118, 105, 108, 115, 32, 97, 110, 100, 32, 103, 111, 111, 100, 32, 100, 101, 118, 105, 108, 115, 46]]

Next Article: Python: 3 Ways to Convert a String to Binary Codes

Previous Article: Python: Converting a string to bytes and vice versa

Series: Working with Strings in Python

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types