
Python Programs for Practice
Python project to convert words to binary code
Project description
Write a python program that asks the user to enter a word and convert it to binary code. Do not use the “bin()” built-in function, build it yourself. The program will work as follows:
Enter a word:angel
## Expected output:
11000011101110110011111001011101100
It is one of Practity’s fun Python programs for practice to get familiar with such an important concept as the binary system. It is also a useful Python programming challenge to learn to break down a problem in several parts and to explore how built-in functions work. In this Python programming problem you will have to go without an already existing Python function.
Binary is a numerical system that uses only two digits, 0 and 1, to represent information. It is widely used in computer systems and digital communication because it can easily represent and manipulate data electronically. In binary, each digit, or bit, represents a power of two. By assigning 0s and 1s to each bit, we can represent numbers, characters, and other forms of data. To show a letter as 0 and 1, one common approach is to use the ASCII (American Standard Code for Information Interchange) encoding system. In ASCII, each character is assigned a unique numeric value, which can be represented in binary. By converting the ASCII value of a letter to binary, we can display it as a sequence of 0s and 1s.
Steps
Steps to complete this Python project:
- Define a function
"convert_to_binary(word)"that takes a word as input. - Inside the function, create an empty list
"binary_list"to store the binary values. - Use a
forloop to iterate through each character in the word. - For each character, convert it to its ASCII value using the
"ord()"function. - Initialize a variable
"decimal"with the ASCII value. - Create an empty string
"binary"to store the binary representation. - Implement a while loop with the condition
decimal > 0. - Inside the while loop, perform the following steps:
- Calculate the remainder of
decimaldivided by 2 using the modulo operator%. - Convert the remainder to a string using the
str()function. - Concatenate the remainder to the beginning of the
binarystring. - Update the value of
decimalby integer division (//) with 2.
- Calculate the remainder of
- Append the
"binary"string to the"binary_list". - Return the
"binary_list"from the function. - Prompt the user to enter a word using the
"input()"function and store it in a variable"user_word". - Call the
"convert_to_binary()"function with"user_word"as the argument and store the result in a variable"binary_values". - Print the
"binary_values"to display the binary representation of the word.
Try other real Python projects for intermediates and keep practicing.
Source Code
## Define de variables
def convert_to_binary(word):
binary_list = []
for char in word:
decimal = ord(char)
binary = ""
while decimal > 0:
binary = str(decimal % 2) + binary
decimal = decimal // 2
binary_list.append(binary)
return binary_list
user_word = input("Enter a word: ")
binary_values = convert_to_binary(user_word)
print(binary_values)