Text Shift Function In Python
Solution 1:
You are looping over the list of characters, and i
is thus a character. You then try to store that back into data
using the i
character as an index. That won't work.
Use enumerate()
to get indexes and the values:
defshifttext(shift):
input=raw_input('Input text here: ')
data = list(input)
for i, char inenumerate(data):
data[i] = chr((ord(char) + shift) % 26)
output = ''.join(data)
return output
You can simplify this with a generator expression:
defshifttext(shift):
input=raw_input('Input text here: ')
return''.join(chr((ord(char) + shift) % 26) for char ininput)
But now you'll note that your % 26
won't work; the ASCII codepoints start after 26:
>>>ord('a')
97
You'll need to use the ord('a')
value to be able to use a modulus instead; subtracting puts your values in the range 0-25, and you add it again afterwards:
a = ord('a')
return''.join(chr((ord(char) - a + shift) % 26) + a) for char in input)
but that will only work for lower-case letters; which might be fine, but you can force that by lowercasing the input:
a = ord('a')
return''.join(chr((ord(char) - a + shift) % 26 + a) forcharininput.lower())
If we then move asking for the input out of the function to focus it on doing one job well, this becomes:
def shifttext(text, shift):
a = ord('a')
return ''.join(chr((ord(char) - a + shift) % 26 + a) for char in text.lower())
print shifttext(raw_input('Input text here: '), 3)
and using this on the interactive prompt I see:
>>>print shifttext(raw_input('Input text here: '), 3)
Input text here: Cesarsalad!
fhvduvdodgr
Of course, now punctuation is taken along. Last revision, now only shifting letters:
def shifttext(text, shift):
a = ord('a')
return''.join(
chr((ord(char) - a + shift) % 26 + a) if'a' <= char <= 'z'elsecharforcharin text.lower())
and we get:
>>>print shifttext(raw_input('Input text here: '), 3)
Input text here: Ceasarsalad!
fhdvduvdodg!
Solution 2:
Looks you're doing cesar-cipher encryption, so you can try something like this:
strs = 'abcdefghijklmnopqrstuvwxyz'#use a string like this, instead of ord()
def shifttext(shift):
inp = raw_input('Input text here: ')
data = []
for i in inp: #iterate over the text not some listif i.strip() and i in strs: # if the char is not a space ""
data.append(strs[(strs.index(i) + shift) % 26])
else:
data.append(i) #if space the simply append it to data
output = ''.join(data)
return output
output:
In [2]: shifttext(3)
Input text here: how are you?
Out[2]: 'krz duh brx?'
In [3]: shifttext(3)
Input text here: Fine.
Out[3]: 'Flqh.'
strs[(strs.index(i) + shift) % 26]
: line above means find the index of the character i
in strs
and then add the shift value to it.Now, on the final value(index+shift) apply %26 to the get the shifted index. This shifted index when passed to strs[new_index]
yields the desired shifted character.
Solution 3:
Martijn's answer is great. Here is another way to achieve the same thing:
importstring
def shifttext(text, shift):
shift %= 26 # optional, allows for |shift| > 26
alphabet = string.lowercase # 'abcdefghijklmnopqrstuvwxyz' (note: for Python 3, use string.ascii_lowercase instead)
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
returnstring.translate(text, string.maketrans(alphabet, shifted_alphabet))
print shifttext(raw_input('Input text here: '), 3)
Solution 4:
It's easier to write a straight function shifttext(text, shift)
. If you want a prompt, use Python's interactive mode python -i shift.py
> shifttext('hello', 2)
'jgnnq'
Solution 5:
Tried with Basic python. may useful for someone.
# Caesar cipherimport sys
text = input("Enter your message: ")
cipher = ''try:
number = int(input("Enter Number to shift the value : "))
except ValueError:
print("Entered number should be integer. please re0enter the value")
try:
number = int(input("Enter Number to shift the value : "))
except:
print("Error occurred. please try again.")
sys.exit(2)
for char in text:
ifnot char.isalpha():
flag = char
elif char.isupper():
code = ord(char) + number
if64 < code <= 90:
flag = chr(code)
elif code > 90:
flag = chr((code - 90) + 64)
elif char.islower():
code = ord(char) + number
if96 < code <= 122:
flag = chr(code)
elif code > 122:
flag = chr((code - 122) + 96)
else:
print("not supported value by ASCII")
cipher += flag
print(cipher)
Post a Comment for "Text Shift Function In Python"