Making a new function

can yall make a new function that lets people turn strings into ints not like a str like this “81” but a str like this “ha” and then the function could be called stint for string to int and when you pass “ha” into stint it would become 81 for there numbers in the alphabet and a space would be 27 and then if you wanted to you could make it so it can go back to a str so 81 would be ha please make this i think it would be cool=)

Sounds like a cool programming challenge for you to do yourself! Read about the ord function for some starting thoughts. Also worth considering: should case matter? What should the function do with non-letter inputs?

Moving to the general Help category as this does not appear to be a serious idea for the Python language in need of discussion.

Case would not matter the functions would make all the letter lowercase and for things like this ! You could either add more numbers so like 28 or you could make the function check to see if all the characters are OK but if they are not you can give an error like error character not supported i made some code for it before here it is =):


def stint(str2 = “”): # this means string to int

numsfs = {#this means nums for strings

"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,

" ": 0

}

strtr = str2.lower() #strtr means str to read 

fulls = "" #this means full sentence

for i in strtr:

    if i in numsfs:

        if i == " ":

            num = 0

            fulls += str(num)

        elif i in numsfs:

            num = ord(i) - 96

            fulls += str(num)

return int(fulls)

There is a function to turn strings into ints, and it’s called hash.[1] It’s a buildin and does not need to be imported.


  1. It’s probably now what you want, but describing what’s wrong with it will give you a spec that describes the function you want. And then you can hopefully code it yourself. ↩︎

Huh I did not know that thanks for telling me that is cool=)