Python Project to Convert Words to Binary

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:

  1. Define a function "convert_to_binary(word)" that takes a word as input.
  2. Inside the function, create an empty list "binary_list" to store the binary values.
  3. Use a for loop to iterate through each character in the word.
  4. For each character, convert it to its ASCII value using the "ord()" function.
  5. Initialize a variable "decimal" with the ASCII value.
  6. Create an empty string "binary" to store the binary representation.
  7. Implement a while loop with the condition decimal > 0.
  8. Inside the while loop, perform the following steps:
    • Calculate the remainder of decimal divided by 2 using the modulo operator %.
    • Convert the remainder to a string using the str() function.
    • Concatenate the remainder to the beginning of the binary string.
    • Update the value of decimal by integer division (//) with 2.
  9. Append the "binary" string to the "binary_list".
  10. Return the "binary_list" from the function.
  11. Prompt the user to enter a word using the "input()" function and store it in a variable "user_word".
  12. Call the "convert_to_binary()" function with "user_word" as the argument and store the result in a variable "binary_values".
  13. 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)

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for practice
Register New Account
Shopping cart