Change Each Character In String To The Next Character In Alphabet
Solution 1:
I think you are making this too complicated.
Just use modulo to roll around to the beginning of the string:
from stringimport ascii_letters
s='abcxyz ABCXYZ'
ns=''for c in s:
if c in ascii_letters:
ns=ns+ascii_letters[(ascii_letters.index(c)+1)%len(ascii_letters)]
else:
ns+=c
Which you can reduce to a single unreadable line if you wish:
''.join([ascii_letters[(ascii_letters.index(c)+1)%len(ascii_letters)]ifcin ascii_letters elsecforcin s])
Either case,
Turns abcxyz ABCXYZ
into bcdyzA BCDYZa
If you want it to be limited to upper of lower case letters, just change the import:
from string import ascii_lowercase as letters
s='abcxyz'
ns=''forcin s:ifcinletters:
ns=ns+letters[(letters.index(c)+1)%len(letters)]else:
ns+=c
Solution 2:
Two main things. 1) Don't use the Python built-in str
to define variables as it could lead to unusual behaviour. 2) for letter in range(len(str))
does not return a letter at all (hence the error stating that 0 is not in your list). Instead, it returns numbers one by one up to the length of str
. Instead, you can just use for letter in my_string
.
EDIT: Note that you don't need to convert the string into a list of letters. Python will automatically break the string into individual letters in for letter in strng
. Updated answer based on comment from linus.
def LetterChanges(strng):
ab_st = list(string.lowercase)
output_string = []
for letter in strng:
if letter == 'z':
output_string.append('a')
else:
letter_index = ab_st.index(letter) + 1
output_string.append(ab_st[letter_index])
new_word = "".join(output_string)
return new_word
# keep this function call here
print LetterChanges(raw_input())
Solution 3:
this is my code i think it is very simple
def LetterChanges(st):
index = 0
new_word = ""
alphapet = "abcdefghijklmnopqrstuvwxyzacd"for i in st.lower():
if i.islower(): #check if i s letterindex = alphapet.index(i) + 1#get the index of the following letter
new_word += alphapet[index]
else: #if not letter
new_word += i
return new_word
print LetterChanges(raw_input())
Solution 4:
The problem you are solving is of Ceaser cipher. you can implement the formula in your code.
E(x) = (x+n)%26 where x is your text and n will be the shift.
Below is my code. (I write the code in python 3)
import ast
n = ast.literal_eval(input())
n1 = n[0]
step = n[1]
defenc_dec(string,step):
result = ''for i in string:
temp = ''if i=='':
result = result+i
elif i.isupper():
temp = chr((ord(i) + step - 65) % 26 + 65)
else:
temp = chr((ord(i) + step - 97) % 26 + 97)
result = result + temp
return result
print(enc_dec(n1,step))
Solution 5:
There's two issues with the code. Instead of looping letters you're looping over numbers since you're calling range(len(str))
. The second issue is that within the loop you assign a string to new_word
which will cause the next iteration to fail since string
doesn't have method append
. If you make the following changes it should work:
for letter in str: # for letter in range(len(str)):
if letter == "z":
new_word.append("a")
else:
new_word.append(ab_st[str.index(letter) + 1])
# new_word = "".join(new_word)
new_word = "".join(new_word)
Post a Comment for "Change Each Character In String To The Next Character In Alphabet"