[Tutor] How to change the char in string for Python
Jos Kerc
joskerc at gmail.com
Sun Feb 24 15:12:23 CET 2013
On Sun, Feb 24, 2013 at 2:26 PM, Sudo Nohup <sudo.nohup at gmail.com> wrote:
> Thanks very much!!
>
> I learnt a lot from you kind reply. Not only is it about the question
> itself, but also about how to ask a question in a mailing list.(Sorry that
> it is the first time for me to ask questions in a mailing list).
>
> The question comes from a riddle of the PythonChallenge website(
> http://www.pythonchallenge.com/pc/def/map.html).
>
> Now I write the code as the following,
>
> mystring = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq
> ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm
> jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
> temp = [ chr ( (ord(char)-ord('a')+2)%26 +ord('a')) if
> (ord(char)>=ord('a') and ord(char)<=ord('z')) else char for char in
> mystring ]
> result = "".join(temp)
> print result
>
> OR
>
> mystring = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq
> ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm
> jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
> result = []
> for char in mystring:
> if(ord(char)>=ord('a') and ord(char)<=ord('z')):
> char = chr ( (ord(char)-ord('a')+2)%26 +ord('a'))
> result.append(char)
> result = "".join(result)
> print result
>
> Thanks,
> James
>
>
>
>
Hi James,
for this riddle, look for the translate() method.
Have fun nwith the challenges.
Jos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130224/57a8f5e4/attachment.html>
More information about the Tutor
mailing list