0

I am trying to manipulate user input strings using the ord() funtion. This effectively allows me to identify the decimal value of each character in the string. I have been able to return the value of the characters, but what I need to understand is how to store each value (to manipulate it), and how to return the characters associated with the new values.

In essence, the user inputs :

>>>cat

and the return is

99
97
116

I want to store these values, then manipulate them:

99+1=100
97+14=111
116-13=103

then returns:
'dog'

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Carlo Gambino
0

Fun with ascii values of a string ...

def ascii_list(s):
    """return a list of ascii values of the characters in string s"""
    return [ord(c) for c in s]

s = 'cat'
alist = ascii_list(s)
print alist  # [99, 97, 116]

# manipulate the list elements
alist[0] += 1
alist[1] += 14
alist[2] -= 13

# convert ascii to char
clist = [chr(e) for e in alist]
# join to form a word
print "".join(clist)  # dog
0

Extremely helpful, thanks for the post! And finally the use of char() sinks into my thick skull.

I'm greatful to all your contributions.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.