Secret Message - 2
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.
a b c d e f g h i j k l m n o p q r s t u v w x y z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Input
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".
Sample Input 1
python
Sample Output 1
16-25-20-8-15-14
Sample Input 2
Foundations
Sample Output 2
6-15-21-14-4-1-20-9-15-14-19
Source code
alphabet="abcdefghijklmnopqrstuvwxyz"
numbers=[i for i in range(1,len(alphabet)+1)]
def char_to_int(char):
for i in range(len(alphabet)):
if char.lower()==alphabet[i]:
return numbers[i]
if char.lower() not in alphabet:
return char
s=input()
s_num=[]
for i in s:
s_num.append(char_to_int(i))
for i in range(len(s_num)):
if i==(len(s_num)-1):
print(s_num[i],end="")
else:
print(s_num[i],end="-")
Output
Comments
Leave a comment