Answer to Question #188200 in Python for Hari nadh babu

Question #188200

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.


Input:-

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.


For Example -

Input 1:-

python


Output 1:-

16-25-20-8-15-14


Input 2:-

Foundations

Output 2:-

6-15-21-14-4-1-20-9-15-14-19


Input 3:-

python learning

Output 3:-

16-25-20-8-15-14 12-5-1-18-14-9-14-7

Note:- there is a space between two strings that means 16-25-20-8-15-14 12-5-1-18-14-9-14-7


We want given both three inputs they can get both three outputs we code was run one by one input and output


1
Expert's answer
2021-05-05T08:50:30-0400


import string
code = {}
for i, ch in enumerate(string.ascii_letters):
    code[ch] = i+1

inpt = input()

wasCode = False
for ch in inpt:
    ch = ch.lower()
    if ch in code:
        if wasCode:
            print('-', end='')
        print(code[ch], end='')
        wasCode = True
    else:
        print(ch, end='')
        wasCode = False
print()

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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS