Q:

Google App Engine, changing character on ASCII table

I am trying to do the following:
In an HTML TextArea, change the position of each character of the entered text in the ASCII table by 13 positions (in my code it is rot13 function).
Here's the Rot13 function:
def rot13(s):
    for a in s:
       print chr(ord(a)+13)

It does work this way, but it prints as well header information and omits the last character. So when I enter the word 'Hello' in the box, the result is:
U r y y | Status: 200 Content-Type: text/html; charset=utf-8 Cache-Control: no-cache Content-Length: 4 None

So how to go about this?
Also, when I tried to do it this way:
def rot13(s):
    for a in s:
       chr(ord(a)+13)
    return s

It returned the same text I've put in the text, without the changes I thought there would be. So as I understand, it doesn't modify the 's' directly that way? So how to go about this?
The whole code is below:
import webapp2
import cgi
def escape_html(s):
    return cgi.escape(s, quote = True)

form = """<html>
<head><title>Rot13</title></head>
<body>
<form method="post">
Please enter your text here:<br>
<textarea name="text"></textarea>
<input type="submit">
</form>
</body>
</html>
"""

def rot13(s):
    for a in s:
       print chr(ord(a)+13)

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write(form)

    def post(self):
        entered_text = self.request.get('text')
        self.response.out.write(rot13(entered_text))

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

A:

EDIT: Here is a function for performing an actual ROT13 on a string (my apologies - I wasn't aware this was an actual algorithm - the method I provided in the previous answer will not perform true ROT13). However if you are looking for an efficient way to implement it, you are going to have a hard time beating @NickJohnson's answer :) This translates according to table here, with uppercase mapping to uppercase and lowercase mapping to lowercase. This creates a mapping of uppercase and lowercase letters, mapped to their ROT13 equivalents by splitting the component parts in half and flipping them. You then zip the 'normal' and 'reversed' lists together and create a dictionary from the key/value pairs that are generated:
In [1]: import string

In [2]: w = 'SecretWord'

In [3]: uc, lc = string.uppercase, string.lowercase

In [4]: c_keys = uc + lc

In [5]: c_val = uc[13:] + uc[:13] + lc[13:] + lc[:13]

In [6]: d = dict((k, v) for k, v in zip(c_keys, c_val))

In [7]: ''.join(d[i] for i in w)
Out[7]: 'FrpergJbeq'

And to hopefully make it easier to visualize, here is what the key/value strings look like:
In [8]: c_keys
Out[8]: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

In [9]: c_val
Out[9]: 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'

Instead of using print, you'll want to use self.response.out.write (as you have in other places). This will write the result to the HTTP response that is sent to the requester (note that headers are part of the response as well, but aren't printed to the screen unless specified when using self.response.out.write). In your case, a simplified version could look something like this:

def rot13(s):
    return ''.join(chr(ord(a)+13) for a in s)

Instead of printing each character, it constructs a string using the function you defined. This will return that string, and the result will be written out as a response. You could also add additional HTML formatting etc., but this should hopefully get you on the right track.
Also, regarding why your second function didn't work:
def rot13(s):
    for a in s:
       chr(ord(a)+13)
    return s

The reason that it returns s unaltered is because although you are performing the chr(ord(a)+13) step, you aren't doing anything with it. Your function is definitely on the right track, so one way to make it return identical output to the one in the example above is to create an empty list that will store each modified character, and then join the list at the end:
def rot13(s):
    # This is your container
    l = []
    for a in s:
       # Now, instead of just running the function, we add the result to the list
       l.append(chr(ord(a)+13))
    # And finally, we use join to join all the list elements together and return
    # E.g. If your list is ['a', 'b', 'c'], this returns the string 'abc'
    return ''.join(l)

