How to print the previous alphabets of a given string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to print the previous alphabets of a given string?

Eg. If the Input is 'CAT’ the output will be 'BZS'. DOG-> CNF

30th Sep 2020, 10:11 AM
Saumyadip .Kumar
Saumyadip .Kumar - avatar
7 Answers
30th Sep 2020, 1:31 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 8
A bit late, but i still want to share this code with you: - the "replacement / movement" can be done in any direction by using positive or negativ numbers - from module string the uppercase letters are used. this does not create an overhead, it just creates a variable with the alphabet - the alphabet is used and rotated by the value of "move" - a dictionary is created by using zip() with original alphabet and the rotated version - the replacement is done in a list comprehension *** this code can also be used to encrypt / decrypt text. in this case a shuffeld alphabet can be used instead of an ordered alphabet https://code.sololearn.com/c1rNe8FqRU87/?ref=app
3rd Oct 2020, 7:58 AM
Lothar
Lothar - avatar
+ 4
this works with both small and capital letters.. a="CAT" b="" for i in a: b += chr(ord(i)+25) if(i=='A' or i=='a') else chr(ord(i)-1) print(b)
30th Sep 2020, 10:42 AM
Rohit
+ 3
a="CAT" b="" for i in a: if ord(i)==65: b+=chr(90) continue b+=chr(ord(i)-1) print(b)
30th Sep 2020, 10:21 AM
Abhay
Abhay - avatar
+ 3
Thank you guys.got it
30th Sep 2020, 11:52 AM
Saumyadip .Kumar
Saumyadip .Kumar - avatar
+ 2
Another way will be to put the alphabet in a list or array and choose a previous index to the character input.
30th Sep 2020, 10:51 AM
JaScript
JaScript - avatar
+ 1
I'm kind of a beginner so I didn't quite get Vitaly's code. I'm going with this https://code.sololearn.com/c83qYy6QnfOh/?ref=app
30th Sep 2020, 3:52 PM
Saumyadip .Kumar
Saumyadip .Kumar - avatar