How can i replace words to alphabet in input ? | Sololearn: Learn to code for FREE!

0

# How can i replace words to alphabet in input ?

https://code.sololearn.com/cKDHbTMY754F/?ref=app Like a = 1 , b = 2 , c = 3 and more.. For example i input abc, And i want to get 6 in output. How it’s gonna work ? I want to learn ,pls🐌

7/3/2022 12:32:55 PM

Kimia

+9

You'd most likely have to have a dictionary with those values, ex: data = {'a': 1, 'b': 2, ...} Then you split string, access data dict with each letter and add the key's value to total.

+4

You can try use of ascii values there.. 'a' ascii value is 97 'z' ascii value is 97+26 ord() function returns ascii value. Ex: ord('a') returns 97 , 'a' ascii value.. ord('a') - 97+1 returns 1 ord('z') - 97 +1 returns 26 By using loop, you can find sum.. With @slick solution of using dictionary and mapping dict values is like: With having dictionary, replace 'a' with dict('a') so 'abcd' becomes '1234'. hope it helps...

+3

Kimia If input is 'a3b8hk' then what is your expected output? briefly how?

+2

What questions do you have about my answer? It gives just enough away to point you in the right direction without just giving away the answer.

+1

Okay, do you know about dictionaries and how to make and access them? Do you know about the <string>.split() function? Do you know about loops? We need to know where you're at to help guide you to where you're going.

+1

Take the big problem and break it into smaller problems. You don't need to code it all in one line. First, do it on a piece of paper than take the steps you used and translate them to code. Give it a shot and post the attempt

+1

Kimia As I said it before, Ex: ord('h') -97+1 returns 11 You can replace it with h You can repeat this within a loop. If you want to use dictionary, then learn about dictionary. It will like this ex: dic = { 'a' : 1, 'b':2, .... 'z':26 }; replace( 'a', dic('a') ) will replace 'a' with 1 (*sample way only, not exact syntax). Before replacing, check : if (not 'a'.isdigit() ) so that only alphabet gets replace...

+1

This is an example in Python (everything is very detailed explained): # this links each letter of the alphabet to its corresponding numbers: a = 1, b = 2, etc... alphabet={"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14,"o":15,"p":16,"q":17,"r":18,"s":19,"t":20,"u":21,"v":22,"w":23,"x":24,"y":25,"z":26} # gets the input and assigns it to inp inp=input() # assigns the varable output to an empty string output="" # creates a loop through the input and creates the variable n (for example: if the input is "hi", the first time n equals "h" and the second time n equals "i".) for n in inp: # adds the number (see where the variable alphabet is defines) that is linked to n to the output output+=alphabet[n] # prints the output print(output)

+1

Stack overflow has a plethora of solutions for your question. Only 2 of them actually worked-- so here's the link, and I'll post the snippets of the 2 that I ran in py2 & py3 w/success... https://stackoverflow.com/questions/58202686/replacing-letters-with-numbers-in-python-objects

+1

course.to_string() string_to_change = 'abc123' mapping_dict = dict(zip(['a','b','c'],['1','2','3'])) for k, v in mapping_dict.iteritems(): string_to_change = string_to_change.replace(k,v) '''

+1

Maybe just go to the link for ςτλςκθvεr₣₤øω I posted above 🥳

+1

dict(enumerate("abc",1)) could make the dict easier to create

+1

https://code.sololearn.com/cMjDGSxG3GL4/?ref=app

0

Slick Can you explain more ?🐁

0

Slick I mean i don’t have idea how is working, like how can i call data in input for convert to num for sum🐌

0

Slick i know <string>.split() function but i don’t know about access dic🌚

0

Jayakrishna🇮🇳 I don’t want ascii value i just want replace alphabet words to num, and it’s working everywhere like : a3b8hk

0

In the case you just mentined, you'd have to then just use list() on a string to turn it into a list of characters. Then you'd have to use <character>.isdigit() to check if the string value is a number. If it's not a number, use the character to access the data dictionary and replace the index with the cooresponding value. Then at the end, sum up all the numbers by changing everything in the list to an int() and add them all up https://code.sololearn.com/cuyOWt8VihF8/?ref=app

0

Slick i just confused i think it will make me crazy🐢

0

Jayakrishna🇮🇳 input = (a3b8hk) It’s 1328811 output = 6