A quick question

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed May 28 06:48:02 EDT 2008


James a écrit :
> Hey everyone,
> 
> I just started using python and cant figure this out, I'm trying to 
> make a program where someone types in a word and the program gives it 
> back backwards.  For example if the person puts in "cat" I want the 
> program to give it back as "tac" and what it does is prints out 3,2,1.  

This is what you asked for - range() returns a list of integers.

> How can I get these integers to print as letters?

What you want is not to "get these integers as letters", but to get the 
letters in the word in reversed order.

>  This is what I have,
> 
> word = raw_input("Type a word:")
> start = len(word)
> 
> for letter in range(start, 0, -1):
>     print letter  
> 

- The naive version:

for i in range(start-1, -1, -1):
     print word[i]


- The naive version with a native attempt to correct (wrt/ your specs) 
formatting:

drow = ''
for i in range(start-1, -1, -1):
     drow += word[i]
print drow

- The canonical pythonic version:

for letter in reverse(word):
     print letter

- The canonical version with correct (wrt/ your specs) formatting:

print ''.join(reversed(word))


- The Q&D (but working) perlish version:

print word[::-1]


HTH



More information about the Python-list mailing list