How to remove string accents using Python 3?

String accents are special string characters adapted from languages of other accents. In this article, we are going to remove ascents from a string.

Examples:

Input: orčpžsíáýd

Output: orcpzsiayd

Input: stävänger

Output: stavanger

We can remove accents from the string by using a Python module called Unidecode. This module consists of a method that takes a Unicode object or string and returns a string without ascents.

Syntax:

output_string = unidecode.unidecode(target_string )

Below are some examples which depict how to remove ascents from a string:

Example 1:

Python3




# import required module
import unidecode
 
# assign string
string = "orčpžsíáýd"
 
# display original string
print('\nOriginal String:', string)
 
# remove ascents
outputString = unidecode.unidecode(string)
 
# display new string
print('\nNew String:', outputString)


Output:

Original String: orčpžsíáýd

New String: orcpzsiayd

Example 2:

Python3




# import required module
import unidecode
 
# assign string
string = "stävänger"
 
# display original string
print('\nOriginal String:', string)
 
# remove ascents
outputString = unidecode.unidecode(string)
 
# display new string
print('\nNew String:', outputString)


Output:

Original String: stävänger

New String: stavanger

Example 3:

Python3




# import required module
import unidecode
 
# assign string
stringList = ["hell°""tromsø""stävänger", "ölut"]
 
# display original string
print('\nOriginal List of Strings:\n', stringList)
 
for i in range(len(stringList)):
    # remove ascents
    stringList[i] = unidecode.unidecode(stringList[i])
 
# display new string
print('\nNew List of Strings:\n', stringList)


Output:

Original List of Strings:
 ['hell°', 'tromsø', 'stävänger', 'ölut']

New List of Strings:
 ['helldeg', 'tromso', 'stavanger', 'olut']

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button