Replace every character of string by character whose ASCII value is K times more than it

+1 vote

Secret Message


You write all your passwords in a diary so that you don't forget them. But clearly this is too risky, so you came up with a simple plan, you will simply write it by shifting all the alphabets by a certain step. For eg: if you decide your step to be 3, then 'a' will become 'd', and 'k' will become 'n' and so for all alphabets. The last alphabets will simply circle back to 'a'. In this case, 'y' will become 'b' and so on. Now you just have to remember the step size, can then you can check the password anytime you want. You decided to write code to do this, now that you have learned coding in python. Your code will take in the step size and what is written in the diary and give out the real password.

A list of two elements.
The first element will be a string consisting of only alphabets which is taken from the diary and the second element will be the step size.

A string denoting the password

Sample input:
['ytKveh' 4]

Sample output:

The password was 'upGrad'. Upon changing it by step of 4,
u became y,
p became t,
G became K,
r became v,
a became e,
d became h,
and thus what was written in the diary was ytKveh 

Sample input:
['banana', 7]

Sample output:

Execution time limit


Dec 2, 2019 in Python by Vaibhav
• 130 points
import ast
#start writing your code from here
def shift_string(message, n):
    for i in message:
        ch = i
        base = ord('a' if ch.islower() else 'A')
        x = chr((ord(ch) - base - n) % 26 + base)

2 answers to this question.

0 votes

Hi @Vaibhav! In such a case what you can do is, 

ask the user for the password(which will be in the string format)

take each letter of the string and convert it into a character and use the ord() which will return its Unicode.

To this Unicode, you can add the count value and convert it back to a string.

Finally, print it'

You can use the following code:

password = input("Enter the password")
count = input("Enter the count")
final_pass = ""
for x in list(password):
    y = chr(ord(x) + count)
    final_pass = final_pass + y
answered Dec 5, 2019 by Kalgi
• 52,360 points
it does't works for i/p banana and 7
This will give issue when the last alphabet is Z and the requirement is to have only alphabets in the output.
0 votes

Refer below code for your problem statement

def swap_letters(word, step_count):
    final_word = ""
    for x in list(word):
#         for lowercase letters crossing the Ascii values
        if(ord(x)-step_count<97 and ord(x)>96):
            w = 123-abs(97-(ord(x)-step_count))
            y = chr(w)
            final_word = final_word + y
#           for uppercase letters crossing the Ascii values
        elif(ord(x)-step_count<65 and ord(x)<91):
            w = 91-abs(65-(ord(x)-step_count))
            y = chr(w)
            final_word = final_word + y
#           for normal letters within the Std Ascii Values
            y = chr(ord(x)-step_count)
            final_word = final_word + y
    return final_word

answered May 17, 2020 by Kumar Sambhawam

edited May 18, 2020 by Gitika

Related Questions In Python

0 votes
1 answer

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Hello @kartik, Since different users might have different ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,880 points
0 votes
0 answers

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I couldn't figure out the problem. here my ...READ MORE

Dec 17, 2020 in Python by muammer
• 120 points
0 votes
1 answer

Python join: why is it string.join(list) instead of list.join(string)?

950down voteaccepted It's because any iterable can be ...READ MORE

answered May 15, 2018 in Python by aryya
• 7,450 points
0 votes
1 answer

Python join: why is it string.join(list) instead of list.join(string)?

This is because join is a "string" ...READ MORE

answered Jul 30, 2018 in Python by Priyaj
• 58,090 points
0 votes
1 answer

Why is it string.join(list) instead of list.join(string)?

It's because any iterable can be joined ...READ MORE

answered Dec 2, 2020 in Python by Gitika
• 65,910 points
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,220 views
0 votes
1 answer
+7 votes
8 answers

Difference for string comparison in Python: 'is' vs. ==

If we use "==" means both variables ...READ MORE

answered Sep 3, 2018 in Python by Parul Raheja
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP