Python – Reverse Shift characters by K
Given a String, reverse shift each character according to its alphabetic position by K, including cyclic shift.
Input : test_str = ‘bccd’, K = 1
Output : abbc
Explanation : 1 alphabet before b is ‘a’ and so on.
Input : test_str = ‘bccd’, K = 2
Output : zaab
Explanation : 2 alphabets before b is ‘z’ (rounded) and so on.
Method : Using maketrans() + upper() + list comprehension + translate() + slicing
In this, we make translation table to each character to its K shifted version using maketrans() and slicing. The upper() is used to handle all the upper case characters, translate() is used to perform translation according to lookup translation table created by maketrans().
The original string is : GeeksForGeeks The converted String : WuuaiVehWuuai
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course