In the previous article, we have discussed Python Program to Divide a String in ‘N’ Equal Parts.
When each character in a string is converted to an ASCII value, a list of corresponding ASCII values for each character is returned. Converting each character in “dce” to an ASCII value, for example, yields [100, 99, 101].
Given a string and the task is to convert each character in a given String to an ASCII Value.
ASCII Characters:
The standard range of ASCII, which stands for American Standard Code for Information Interchange, is “Zero” to “One Hundred and Twenty Seven”.
ASCII codes are used to represent text in computers and other electronic devices. The character-encoding schemes used in most modern telecommunications equipment are based on ASCII.
As a result, everything else falls into the category of “non-ASCII” character.
ord() function: To determine the specific ASCII value of that character.
Examples:
Example1:
Input:
Given String = " btechgeeks"
Output:
The given string after convering each character to an ASCII value : [98, 116, 101, 99, 104, 103, 101, 101, 107, 115]
Example2:
Input:
Given String = "hello this is btechgeeks"
Output:
The given string after convering each character to an ASCII value : [104, 101, 108, 108, 111, 32, 116, 104, 105, 115, 32, 105, 115, 32, 98, 116, 101, 99, 104, 103, 101, 101, 107, 115]
Program to Convert each Character in a String to an ASCII Value.
Below are the ways to convert each character in a given String to an ASCII Value.
Method #1: Using For Loop (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Take an empty list say “ASCII_vals” and store it in another variable.
- Loop the above-given string using the for loop.
- Calculate the ASCII value of the iterator using the ord() function and store it in another variable.
- Append the above obtained ASCII value of the iterator to the above-initialized list “ASCII_vals” using the append() method.
- Print the given string after converting each character to an ASCII value.
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_str = "hello this is btechgeeks" # Take an empty list say "ASCII_vals" and store it in another variable. ASCII_vals = [] # Loop the above-given string using the for loop. for itr in gvn_str: # Calculate the ASCII value of the iterator using the ord() function # and store it in another variable. a = ord(itr) # Append the above obtained ASCII value of the iterator to the above-initialized # list "ASCII_vals" using the append() method. ASCII_vals.append(a) # Print the given string after converting each character to an ASCII value. print("The given string after convering each character to an ASCII value :", ASCII_vals)
Output:
The given string after convering each character to an ASCII value : [104, 101, 108, 108, 111, 32, 116, 104, 105, 115, 32, 105, 115, 32, 98, 116, 101, 99, 104, 103, 101, 101, 107, 115]
Method #2: Using For loop (User Input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Take an empty list say “ASCII_vals” and store it in another variable.
- Loop the above-given string using the for loop.
- Calculate the ASCII value of the iterator using the ord() function and store it in another variable.
- Append the above obtained ASCII value of the iterator to the above-initialized list “ASCII_vals” using the append() method.
- Print the given string after converting each character to an ASCII value.
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. gvn_str = input("Enter some random string = ") # Take an empty list say "ASCII_vals" and store it in another variable. ASCII_vals = [] # Loop the above-given string using the for loop. for itr in gvn_str: # Calculate the ASCII value of the iterator using the ord() function # and store it in another variable. a = ord(itr) # Append the above obtained ASCII value of the iterator to the above-initialized # list "ASCII_vals" using the append() method. ASCII_vals.append(a) # Print the given string after converting each character to an ASCII value. print("The given string after convering each character to an ASCII value :", ASCII_vals)
Output:
Enter some random string = btechgeeks The given string after convering each character to an ASCII value : [98, 116, 101, 99, 104, 103, 101, 101, 107, 115]
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.