×

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

Python Euclidean Distance

Euclidean distance is the distance between two points with whatever of dimensions. We are using NumPy library to find and calculate the Euclidean distance. The NumPy library is used for...

1 minute read.

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below: raw_input()input() What is raw_input() Function? raw_input() function is a built-in function in Python. It is used to...

6 minutes read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

Scrimba python

Scrimba allows you to study whenever and wherever the topics or concepts you want. It also replaces classroom instruction with interactive screencasts, live events, and student-to-student help. Scrimba is an interactive...

3 minutes read.

iobase Python

All I/O flow classes derive from this conceptual base class. Derived classes will have to execute several of the class's abstract data types. The loop method is supported by all members of...

4 minutes read.

Python Deep Copy and Shallow Copy

Assignment statements in Python create bindings between a target and an object, not copies of them. The = operator only makes a new variable that shares the reference to the...

3 minutes read.

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python. Firstly, let us see the similarities between Lists and Tuples. Both are two data types...

4 minutes read.

Python String Methods

Python String Methods Python has a set of built-in string methods. The Python string methods are as follows: capitalize() The string.capitalize() method returns a copy of the string by converting the...

5 minutes read.

Difference between Sort and Sorted in Python

If you new to Python, it must be confusing the distinction between the Sort and Sorted functions. However, it is important to understand the differences in order to use them...

5 minutes read.

How to Print Pattern in Python

How to Print Pattern in Python. Pattern programs are really important for beginners and they are always asked in all the technical interviews. Multiple loops approach is used to print the different...

6 minutes read.

Python Continue Statement

In Python, loops automate and repeat processes in a cost-effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the...

3 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

Anaconda python 3.7 download for windows 10 64-bit

Before installing Anaconda, you need to first know what Anaconda actually is. Anaconda is an IDE (Integrated development environment) platform used to code and executes python programs. Anaconda works both online...

3 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

How to Convert int to str in Python

How to Convert int to str in Python In the last article, we studied how we convert a variable with string datatype to integer datatype and discussed different data types available...

4 minutes read.

Python String find() method

Python String find() method The string.find() method in Python finds the first occurrence of the specified value and returns the lowest index in the string if the substring sub is found else it...

2 minutes read.

Python Write Excel File

Python Write Excel File The Python xlwt module is used to write an excel file and perform multiple operations on it. It can be used to write text, numbers, and formulas for multiple...

3 minutes read.

What is the Python Global Interpreter Lock?

Introduction When working with processes, Python employs a form of process lock called the Global Interpreter Lock (GIL). Python typically executes a collection of typed statements using just one thread. It...

4 minutes read.

What is online python free IDE

An IDE is an acronym that stands for “Integrated development environment.” It is a software environment that has a software development package that consists of the code editor and builds...

4 minutes read.

Standard GUI Unit Converter using PyQt5 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...

6 minutes read.