0

I know there's the unicode library, but I don't want to import anything (this is for an assignment where importing libraries loses points).

Let's say I have this string "àèéùùìssaààò" and the desired output would be "aeeuuissaaao". Is there a way to do this in Python?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214

2 Answers2

1

This code should work.

string = "àèéùùìssaààò"
string = string.replace("à", "a")
string = string.replace("è", "e")
string = string.replace("é", "e")
string = string.replace("ù", "u")
string = string.replace("ò", "o")
string = string.replace("ì", "i")
print(string)

you can use string.replace method to replace accents.
you can use string.replace like this string.replace(old, new, [count])

a little easy way

string = "àèéùùìssaààò"
replacelist = ["à", "è" ,"é", "ù", "ò", "ì"] # add the accent to here
correctlist = ["a", "e", "e", "u", "o", "i"] # add the normal English to here
for i in range(len(replacelist)):
    string = string.replace(replacelist[i], correctlist[i])
print(string)

this is using a for loop so it's a little more easy.
you just need to add something to the replacelist, and the correctlist.

ppwater
  • 2,315
  • 4
  • 15
  • 29
  • 1
    There is no way to do that without hardcoding the letters I have to change? – KrankenWagen Nov 06 '20 at 10:08
  • @KrankenWagen Edited so there is more easy way. but I think there is no way to do this without importing something and not hardcoding the letters you have to change. – ppwater Nov 06 '20 at 10:16
1

A fast approach that will scan the string once

string = "àèéùùìssaààò"
lookup = {"à": "a", "è": "e", "é": "e", "ù": "u", "ò": "o", "ì": "i"}
clean_string = ''.join(lookup.get(x, x) for x in string)
print(clean_string)

output

aeeuuissaaao
balderman
  • 22,927
  • 7
  • 34
  • 52