string - Q:python: why is my char considered as a str? -


i started python , i'm stuck on stupid error don't understand why it's not working

def encryption(str1):     = 0     x in str1:         if (x >= 'a' , x <= 'z' or x >= 'a' , x <= 'z'):             str1[i] = str1[i] + 3 % 26 #str[i] = x + 3 % 26         i+=1     return str1 

when execute program got error:

typeerror: string indices must integers, not str.

can explain me why str[i] - 23 considered str ? me i'm modifying ascii value of char.

strings in python not arrays in c. immutable, ie can't make in place changes string. options turn string list (similar c array) or make new empty string , concatenate each encrypted letter onto (this faster). ord converts string ascii number , chr converts inverse. isalpha method tests true iff character in {a-z a-z}

str1 = 'this test string!@#$@#$@'  def encryption(str1):     new_string = ''     character in str1:         if character.isalpha():             new_string += chr(ord(character) + 3 % 26)         else:             new_string += character     return new_string  print(encryption(str1)) 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -