Answer to Question #178129 in Python for ankitha

Question #178129

Given a string, write a program to print a secret message that replaces characters with numbers 'a' with 1, 'b' with 2, ..., 'z' with 26 where characters are separated by '-'.


Note: You need to replace both uppercase and lowercase characters. You can ignore replacing all characters that are not letters.


abcdefghij12345678910

klmnopqr1112131415161718

stuvwxyz1920212223242526Input


The input will be a string in the single line containing spaces and letters (both uppercase and lowercase).Output


The output should be a single line containing the secret message. All characters in the output should be in lower case.Explanation


For example, if the given input is "python", "p" should replaced with "16", similarly"y" with "25","t" with "20","h" with "8","o" with "15","n" with "14". So the output should be "16-25-20-8-15-14".





1
Expert's answer
2021-04-05T23:24:38-0400
def main():
    #Read string
    inputString = input("Enter string: ")
    outputString=""
    for letter in inputString:
        number=getNumber(letter)
        if(number!=-1):
            outputString+=str(number)+"-"
        else:
            outputString+=letter


    print(outputString[:-1])
#This function convert letter to number
def getNumber(letter):
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    for i in range(0,len(alphabet)):
        if letter.islower() and letter==alphabet[i]:
            return i+1
        if letter.isupper() and letter==alphabet[i].upper():
            return i+1
    return -1

main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

krishna vamsi
31.05.21, 09:26

When input is "python learning" output displayed is 16-25-20-8-15-14- 12-5-1-18-14-9-14-7 there is additional - after 14 digit. how to rectify this issue?

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS