perl code to python, alpha character to next alphabet charater?

Fredrik Lundh fredrik at pythonware.com
Fri May 11 07:42:17 EDT 2001


Don Wagner wrote:
> I'm just staring out in python.
> I recently posted for some help about re [A-Za-z0-9] and how to use it in
> Python. I know this code is possible in Python because it is very easy in perl:

I'm afraid Python's not well optimized for tasks like this  (it's an exercise,
right?  if not, I'd love to know why use you have for a program like this),
but something like this might do the trick:

import string, fileinput

lower = string.lowercase
upper = string.uppercase

charmap = string.maketrans(
    upper + lower,
    upper[1:] + upper[0] + lower[1:] + lower[0]
    )

for line in fileinput.input(inplace=1):
    print string.translate(line, charmap)

Cheers /F





More information about the Python-list mailing list