Sling Academy
Home/Python/Python: 3 Ways to Convert a String to Binary Codes

Python: 3 Ways to Convert a String to Binary Codes

Last updated: June 01, 2023

Binary codes (binary, binary numbers, or binary literals) are a way of representing information using only two symbols: 0 and 1. They are like a secret language that computers are able to understand. By combining these symbols in different patterns, we can represent numbers, letters, and other data.

This concise, example-based article will walk you through 3 different approaches to turning a given string into binary in Python.

Using the ord() function and string formatting

What we’ll do are:

  1. Loop through each character in the string.
  2. Convert each character to its corresponding Unicode code using the ord() function.
  3. Convert the Unicode code to binary using the format() function with a format specifier.
  4. Concatenate the binary codes to form the final binary string.

Code example:

input = "Sling Academy"
binary_codes = ' '.join(format(ord(c), 'b') for c in input)
print(binary_codes)

Output:

1010011 1101100 1101001 1101110 1100111 100000 1000001 1100011 1100001 1100100 1100101 1101101 1111001

Using the bytearray() function

Here’re the steps:

  1. Convert the string to a bytearray object, which represents the string as a sequence of bytes.
  2. Iterate over each byte in the bytearray.
  3. Convert each byte to binary using the format() function with a format specifier.
  4. Concatenate the binary codes to form the final binary string.

Code example:

my_string = "Sling Academy"
byte_array = bytearray(my_string, encoding='utf-8')
binary_codes = ' '.join(bin(b)[2:] for b in byte_array)
print(binary_codes)

Output:

1010011 1101100 1101001 1101110 1100111 100000 1000001 1100011 1100001 1100100 1100101 1101101 1111001

Using the bin() and ord() functions

This technique can be explained as follows:

  1. Iterate over each character in the string.
  2. Convert each character to its corresponding Unicode code using the ord() function.
  3. Use the bin() function to convert the Unicode code to binary.
  4. Remove the leading 0b prefix from each binary code.
  5. Concatenate the binary codes to form the final binary string.

Thanks to Python’s concise syntax, our code is very succinct:

text = "Sling Academy"
binary_string = ' '.join(bin(ord(char))[2:] for char in text)
print(binary_string)

Output:

1010011 1101100 1101001 1101110 1100111 100000 1000001 1100011 1100001 1100100 1100101 1101101 1111001

Conclusion

You’ve learned some methods to transform a given string into binary in Python. All of them are neat and delicate. Choose the one you like to go with. Happy coding & have a nice day!

Next Article: 2 ways to get the size of a string in Python (in bytes)

Previous Article: Python: How to Convert a String to Character Codes

Series: Working with Strings in Python

Python

You May Also Like

  • 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
  • Python: Typing a function with *args and **kwargs