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 moduleimport unidecode# assign stringstring = "orčpžsíáýd"# display original stringprint('\nOriginal String:', string)# remove ascentsoutputString = unidecode.unidecode(string)# display new stringprint('\nNew String:', outputString) |
Output:
Original String: orčpžsíáýd New String: orcpzsiayd
Example 2:
Python3
# import required moduleimport unidecode# assign stringstring = "stävänger"# display original stringprint('\nOriginal String:', string)# remove ascentsoutputString = unidecode.unidecode(string)# display new stringprint('\nNew String:', outputString) |
Output:
Original String: stävänger New String: stavanger
Example 3:
Python3
# import required moduleimport unidecode# assign stringstringList = ["hell°", "tromsø", "stävänger", "ölut"]# display original stringprint('\nOriginal List of Strings:\n', stringList)for i in range(len(stringList)): # remove ascents stringList[i] = unidecode.unidecode(stringList[i])# display new stringprint('\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']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


