can somebody help me figure out how to convert a string to a number? Let's say a user inputs "1-800-Flowers" I want python to display "1-800-3569377"
All I have so far is:
userInput = input("Please enter a phone number (e.g. 1-800-Flowers): ")
phoneNum = userInput.replace('-','')
That's really all I have. Any help would be much appreciated
Using str.translate:
>>> pad = ['qz', 'abc', 'def', 'ghi', 'jkl', 'mno', 'prs', 'tuv', 'wxy']
>>> letter_to_numbers = {
... ord(ch): ord(str(i))
... for i, chs in enumerate(pad, 1)
... for ch in chs
... } # Map ord('f') to ord('3'), ...
>>> "1-800-Flowers".lower().translate(letter_to_numbers)
'1-800-3569377'
Have a for loop that loops through the string inputted by the user and then for every letter or number, convert it into its ASCII value using the ord() function.
other=[]
userInput = input("Please enter a phone number (e.g. 1-800-Flowers): ")
for i in userInput:
other+=[ord(i)]
print(other)
What you were saying didn't make sense to me though, as "1-800-Flowers" does not equate to "1-800-3569377" if you convert it to ASCII, you can even check for yourself at the ASCII converter site. Do correct me if what I saying is wrong however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With