Program to Remove any Non-ASCII Characters

Python Program to Remove any Non-ASCII Characters

In the previous article, we have discussed Python Program Enter ‘*’ Between two Identical Characters in a String
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 = 'abc£def'

Output:

The given string after removal of any Non-ASCII Characters = abcdef

Example2:

Input:

Given string = "££abc££kjhf"

Output:

The given string after removal of any Non-ASCII Characters = abckjhf

Program to Remove any Non-ASCII Characters

Below are the ways to remove non-ASCII characters from a given string.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Take a new empty string say ‘new_str’and store it in another variable.
  • Loop in the above-given string using the for loop.
  • Get the ASCII value of the iterator using the ord() function and store it in another variable say ‘numb’.
  • Check if the above-obtained number is greater than or equal to ‘0’ using the if conditional statement.
  • If the statement is true, check again if the given number is less than or equal to ‘127’ using the if conditional statement.
  • If the statement is true, then concatenate the ‘new_str’ with the iterator value and store it in the same variable new_str’.
  • Print the above-given string after removal of any Non-ASCII Characters.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = 'abc£def'
# Take a new empty string say 'new_str'and store it in another variable.
new_str = ""
# Loop in the above-given string using the for loop.
for itror in gvn_str:
    # Get the ASCII value of the iterator using the ord() function and store it in
    # another variable say 'numb'.
    numb = ord(itror)
 # Check if the above-obtained number is greater than or equal to '0' using the
# if conditional statement.
    if (numb >= 0):
        # If the statement is true, check again if the given number is less than or equal to '127'
        # using the if conditional statement.
        if (numb <= 127):
            # If the statement is true, then concat the 'new_str' with the iterator value and
            # store it in the same variable new_str'.
            new_str = new_str + itror
# Print the above-given string after removal of any Non-ASCII Characters.
print("The given string after removal of any Non-ASCII Characters = ", new_str)

Output:

The given string after removal of any Non-ASCII Characters =  abcdef

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 a new empty string say ‘new_str’and store it in another variable.
  • Loop in the above-given string using the for loop.
  • Get the ASCII value of the iterator using the ord() function and store it in another variable say ‘numb’.
  • Check if the above-obtained number is greater than or equal to ‘0’ using the if conditional statement.
  • If the statement is true, check again if the given number is less than or equal to ‘127’ using the if conditional statement.
  • If the statement is true, then concatenate the ‘new_str’ with the iterator value and store it in the same variable new_str’.
  • Print the above-given string after removal of any Non-ASCII Characters.
  • 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 a new empty string say 'new_str'and store it in another variable.
new_str = ""
# Loop in the above-given string using the for loop.
for itror in gvn_str:
    # Get the ASCII value of the iterator using the ord() function and store it in
    # another variable say 'numb'.
    numb = ord(itror)
 # Check if the above-obtained number is greater than or equal to '0' using the
# if conditional statement.
    if (numb >= 0):
        # If the statement is true, check again if the given number is less than or equal to '127'
        # using the if conditional statement.
        if (numb <= 127):
            # If the statement is true, then concat the 'new_str' with the iterator value and
            # store it in the same variable new_str'.
            new_str = new_str + itror
# Print the above-given string after removal of any Non-ASCII Characters.
print("The given string after removal of any Non-ASCII Characters = ", new_str)

Output:

Enter some random string = ££abc££kjhf
The given string after removal of any Non-ASCII Characters = abckjhf

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.