How to remove string accents using Python 3?
Last Updated :
02 Jul, 2021
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 unidecode
string = "orčpžsíáýd"
print ( '\nOriginal String:' , string)
outputString = unidecode.unidecode(string)
print ( '\nNew String:' , outputString)
|
Output:
Original String: orčpžsíáýd
New String: orcpzsiayd
Example 2:
Python3
import unidecode
string = "stävänger"
print ( '\nOriginal String:' , string)
outputString = unidecode.unidecode(string)
print ( '\nNew String:' , outputString)
|
Output:
Original String: stävänger
New String: stavanger
Example 3:
Python3
import unidecode
stringList = [ "hell°" , "tromsø" , "stävänger" , "ölut" ]
print ( '\nOriginal List of Strings:\n' , stringList)
for i in range ( len (stringList)):
stringList[i] = unidecode.unidecode(stringList[i])
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']
Share your thoughts in the comments
Please Login to comment...