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?

4
  • Possibly related: stackoverflow.com/questions/5258623/… Commented Nov 6, 2020 at 9:51
  • This makes me import unicode, but I don't want to import anything Commented Nov 6, 2020 at 9:52
  • 3
    And what exactly is the reason why you don't want to import a module that comes standard with Python? Commented Nov 6, 2020 at 10:08
  • 2
    It's for an assigment, I lose 5 points every time I import something. The score is out of 10. 6/10 is the minimum for pass Commented Nov 6, 2020 at 10:52

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

There is no way to do that without hardcoding the letters I have to change?
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.