String to backwards alphabet | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

String to backwards alphabet

str=input() str=str.lower() talf=โ€œ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 โ€.split(โ€˜,โ€™) rtalf=talf[::-1] for I in range(len(str)): char=str[i] idx = talf.index(char) hide = str.replace(str[i],rtalf[idx]] print(hide) But when Iโ€™m sample input like : Pirate Sample output is : Piratv Itโ€™s like string input effectively change but char by char itโ€™s reset to the start string input so Iโ€™ve only the last char who is convert to backwards alphabet

29th May 2022, 8:59 PM
O'neal
O'neal  - avatar
15 Answers
+ 1
1) You're not using a new empty string to populate it with each iteration's output. So then you have to add space in your list, at both ends, so that you can replace it with itself just by using the same formula, it seems to me. 2)If you used an empty string and just added new character, then an if statement would do it, one as to reverse only if the char is in the alphabet and then add the character inside the loop but outside of if statement so that the space could get added. Here are 3 solutions in this: https://code.sololearn.com/ckWfdFR9k5m8/?ref=app These are just to work with. Lothar'd said slices consume more time, what you see there is just indexing the same alphabet from the end to beginning (as opposed to reversing it and then indexing)
30th May 2022, 12:46 AM
Korkunรง el Gato
Korkunรง el Gato - avatar
+ 2
O'neal MBOULA PENDA There are a lot of characters the code should not change. Instead of trying to deal with spaces in a special way, you could just change the letters, and copy everything else as is.
1st Jun 2022, 2:42 AM
Emerson Prado
Emerson Prado - avatar
+ 1
After removing some other error in code, Use this: and finally print str str = str.replace(str[i],rtalf[idx]) #instead hide = str.replace(str[i],rtalf[idx])
29th May 2022, 9:05 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 1
O'neal MBOULA PENDA As you said you got only wrong output so you are aware of other errors mismatch brace ] ), using wrong quotes for string.. Returns new so store in back on str so you work on same but new modified string in next iteration....
29th May 2022, 9:14 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 1
Your logic don't affect white space. I think its correct to don't affect it. Put as it is.
29th May 2022, 9:20 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 1
Edited the comment in the last part of the code because x.remove() always returns nothing. It changes the list in place. So any assignment is useless. My wording could be misleading, as if it was doable elsewhere.) Also, about your other question: https://code.sololearn.com/c4MluWEfWq1r/?ref=app
30th May 2022, 2:36 AM
Korkunรง el Gato
Korkunรง el Gato - avatar
+ 1
O'neal MBOULA PENDA By all means, pls rename variable "str" - a class you will need has this exact name. Also, in the for loop, you define variable "I", but use variable "i". Make them equal. BTW, instead of splitting with commas, you can just use list("abcdef...").
30th May 2022, 2:42 AM
Emerson Prado
Emerson Prado - avatar
+ 1
O'neal MBOULA PENDA Add your approach to skip space.. index.of method return error when there is no element in list, for which you finding.. So talf.index(' ') result error. And replace function replaces all occurences of string to replace for example "abab" Will first replaced as "zbzb" (a by z) then "zyzy" (b by y) , then "ayay" ( z by a) , last "abab"(y by b) .. Ex: "azaz" => "zzzz" => "aaaa" => "zzzz" => "aaaa" Final string after replace. Use count value to replace the limit for replaces.. As str = str.replace( str[i], rtalf[inx] , 1) #will replace only single character, not all occurences.. May be late, if not solved then , hope it helps..
31st May 2022, 2:05 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
0
I just have to change this ? Of which error are you talking about ? .replace() method does it update string or return a newer
29th May 2022, 9:11 PM
O'neal
O'neal  - avatar
0
Iโ€™m coming to test your proposition and its work verry good hello return svloo But how can i manage if input has whitespeces character
29th May 2022, 9:15 PM
O'neal
O'neal  - avatar
0
Unfortunaly when iโ€™m enterring whitespace program gives an value error : โ€œ โ€ is not in list
29th May 2022, 9:28 PM
O'neal
O'neal  - avatar
0
Iโ€™ve even try to add this : if char == โ€˜ โ€™: continue for ignore โ€˜ โ€˜ character but this time second world does not converted well
29th May 2022, 9:30 PM
O'neal
O'neal  - avatar
0
For example when im sample input : Hello world Sample output : Svloo woild svloo result is correct but the other not
29th May 2022, 9:32 PM
O'neal
O'neal  - avatar
0
Jayakrisna , thx let me try โ€ฆ but if i add : Space character in a both of two list itโ€™s will work too?
31st May 2022, 10:50 PM
O'neal
O'neal  - avatar
0
You can but it depends on how you add, and use those.. Try add at end then see.. But better is to don't touch other except alphabets..
1st Jun 2022, 3:44 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ