×

Convert String to Binary in Python

String to binary

The strings can be defined as the array of Unicode code characters.

Binary

Binary is defined as the number system which consists two symbols 0 and 101. It is base-2 numeral system. The computers perform operations in binary.

The computer views the string in binary format and the binary number is an ASCCI value of string which is converted.  

Method 1: Using ord() function

Firstly, we add the individual ASCII values of the string to a list with the use of ord() function.

The ord function helps in converting the character and gives the string's ASCII value.

Example:

# string initialization  

test_str = "JavaTpoint"

# original string 

print("The original string is : " + str(test_str))

# String to binary conversion

res = ''.join(format(ord(i), 'b') for i in test_str)

# result 

print("The string after binary conversion : " + str(res))

Output:

The original string is : JavaTpoint

The string after binary conversion : 1001010110000111101101100001101010011100001101111110100111011101110100

Method 2: Using bytearray

The bytearray function is used for conversion of string. All characters are mapped into bin using bytearray and it returns the character's array in binary.

Example:

test_str = "JavaTpoint"

# original string 

print("The original string is : " + str(test_str))

# String to binary converting

res = ''.join(format(i, 'b') for i in bytearray(test_str, encoding ='utf-8'))

# result 

print("The string after binary conversion : " + str(res))

Output:

The original string is : JavaTpoint

The string after binary conversion : 1001010110000111101101100001101010011100001101111110100111011101110100

Method 3: Using map() function

By using bytearray, all character can also be mapped into Bin. The function bin() is used for converting decimal number into binary number.

Example:

st = "JavaTpoint"

# original string 

print("The original string is : " + str(st))

result = ' '.join(map(bin, bytearray(st, "utf-8")))

# result 

print("The string after binary conversion : " + str(result))print("The string after binary conversion : " + str(res))

Output:

The original string is : JavaTpoint

The string after binary conversion : 0b1001010 0b1100001 0b1110110 0b1100001 0b1010100 0b1110000 0b1101111 0b1101001 0b1101110 0b1110100

Related Topics

How to add 2 lists in Python?

In Python, a list is defined as a data structure that contains a sequence of elements. It can contain any kind of data type inside it but in order to concatenate two...

3 minutes read.

Removing the First Character from the String in Python

String The string is the collection of characters. The strings in python are “immutable”; we cannot change a string once they are created. In python, whatever data we take as input...

4 minutes read.

Convert XML to JSON in Python

XML conversion is very useful if we work on an API that returns data in JSON format and the source of data is in XML format. JSON A JSON file reserves the...

4 minutes read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

Text detection using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Difference between Perl and Python

Control and presently utilized for a great many undertakings, including framework organization, web improvement, network programming, and GUI improvement, and the sky is the limit from there. About Perl? Perl is a...

4 minutes read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

XXhash Python Examples

XXhash Python: xxHash is an extremely rapid hash calculation that operates inside the confines of RAM. Code is incredibly convenient, and hashes (almost nothing/large endian) are same at all levels. Execution of...

4 minutes read.

Python sklearn train_test_split

Sklearn: One of the most beneficial open-source libraries for learning algorithms in Python is, without a doubt, Sklearn or Scikit-Learn. The most effective tools for statistical modeling and machine learning are all included in...

6 minutes read.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Problem-solving with algorithm and data structures using Python

What is problem-solving? There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise...

3 minutes read.

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

FOO in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python ltrim() function

Python ltrim() function The ltrim() function in PHP removes whitespace or other predefined characters from the beginning or left side of a string. The related functions are as follows: rtrim() – This function is used to...

1 minute read.

Python String lstrip() method

Python String lstrip() method The string. lstrip () method in Python returns a copy of the string with leading characters removed (based on the string argument passed). Syntax string.lstrip([chars]) Parameter chars(optional): This parameter represents a...

1 minute read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Insertion Sort using Python

Insertion sort is a type of sorting technique that is used for sorting an array with random elements. Using sorting methods, any unsorted array can be sorted into ascending or...

3 minutes read.

Python oct() function

Python oct() function The oct() function in Python converts an integer number to an octal string prefixed with “0o”. Syntax oct(x) Parameter x: This parameter represents an Integer Number Return This function returns an octal string. Example 1 #...

1 minute read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.

Latest Project Ideas using Python 2024

Python is one of the well-known languages for programming in the contemporary domain of computer science. The demand to adopt Python for IT purposes is steadily increasing because of its...

7 minutes read.