Python - How To Convert String to ASCII Value in Python

ID : 62

viewed : 261

Tags : PythonPython ASCIIPython String

vote vote

98

This tutorial will introduce some methods to convert a string into ASCII values in Python.

Use the for Loop Along With the ord() Function to Get ASCII of a String in Python

We can use the for loop and the ord() function to get the ASCII value of the string. The ord() function returns the Unicode of the passed string. It accepts 1 as the length of the string. A for loop is used for iterating over a sequence: a list, a tuple, a dictionary, a set, or a string. Therefore, we can use the for loop to parse every character of the string and convert it into ASCII values.

In the code below, text is a variable that holds the user input. ascii_values is an empty list initially, which will hold the ASCII values of each character in the string later. Once the loop has completed its cycle, we will display the contents of ascii_values as output to the user. The append() function adds a new item to the list ascii_values after each iteration.

When we run this program user is prompted with a string, and once the user provides a string, it will be stored in a variable text. In the example, the input is the string hello. The ASCII value of each character of the string is printed.

Example Code:

#python 3.x text = input("enter a string to convert into ascii values:") ascii_values = [] for character in text:     ascii_values.append(ord(character)) print(ascii_values) 

Output:

enter a string to convert into ASCII values: hello [104, 101, 108, 108, 111] 

Use the List Comprehension and the ord() Function to Get ASCII of a String in Python

We can use the list comprehension to achieve the same result. The list comprehension in Python is an easy and compact syntax for creating a list from a string or another list. It is a concise way to create a new list by operating on each item in the existing list. The list comprehension is considerably faster than processing a list using the for a loop.

In the code below, there is no for or while loop externally. But within list comprehension, we are using for loop to get each character of the text.

Example Code:

#python 3.x text = input("enter a string to convert into ascii values: ") ascii_values = [ord(character) for character in text] print(ascii_values) 

Output:

enter a string to convert into ASCII values: hello [104, 101, 108, 108, 111] 

Use a User-Defined Function to_ascii() to Get ASCII of a String in Python

Another way of writing the code to accomplish the same goal is to use a user-defined function. User-defined functions are functions that you use to organize your code in the body of a policy. Once you define a function, you can call it similar to the built-in action and parser functions. Variables that are passed to a function are passed by reference rather than by value.

In the code below, we use a user-defined function to_ascii to take text as a parameter. Inside the function, block operation is defined, and the result is transferred by keyword return. When the function to_ascii is called from the main module providing text as parameter control transfers to the to_ascii function and code block is executed, we get ASCII values of the given string in a list.

Example Code:

#python 3.x def to_ascii(text):     ascii_values = [ord(character) for character in text]     return ascii_values text = input("Enter a string: ") print(to_ascii(text)) 

Output:

Enter a string: hello [104, 101, 108, 108, 111] 

  • Related HOW TO?