This is probably the wrong way to do it, so please teach me the right way. The problem I have with this one is, how do I tell the computer to stop when it hits the last letter? Thank you for your help.

Re: How to write a word backwards

If you're not terribly new to python you'll know about string slicing. [:3] takes everything from a string, starting at the beginning and going up to, but not including, the 3rd character. In Python you can also do extended slicing. [::-1] is of the form [begin:end:step]. Since no parameters are given for the beginning or the end, Python knows that you want to entire string. The -1 tells us that we want to count in reverse.

Re: How to write a word backwards

Posted 02 July 2011 - 05:56 AM

Slicing is extremely powerful, and definitely the right way to do it... but just to follow your code example and do it your way, you need a loop to get each letter rather than manually code each line... so you'd do it something like:

The count - i - 1 part is because, for example, if you have a word which is 5 letters long then count = 5... but indexing of each position starts at zero, meaning the last letter is at position 4 - hence the -1:

word: hello
index:01234
len(word) = 5

Hope that makes sense - welcome to Python, its a very cool language (although no-one who programs in any other language will ever admit that )

Re: How to write a word backwards

Wow, I thought I'd be first to get to this one because it was posted so early in the morning. Brewer's technique is identical to mine. To expand on the technique a little.

string[start:stop:step] most are easy but step can be a little tricky. If you were to do

>>> x = "Hello World!"
>>> x[::2]
'HloWrd'

Since I'm stepping by two's, I'm doing every other letter. -1 means to go backwards, so you'd be starting at the beginning, going to the end, but stepping backwards. I love the step attribute, and have even used it on occasion in production code.

Re: How to write a word backwards

Posted 02 July 2011 - 09:25 AM

I shouldn't do this, but I feel like nit-picking today. The TopicOpener obviously used python 3.x syntax. But The answers given by kiwi_steve and Brewer were in python 2.x syntax .
Luckily the TopicOpener didnt't get confused by that .

Re: How to write a word backwards

Posted 02 July 2011 - 11:04 PM

Nallo, on 03 July 2011 - 05:25 AM, said:

I shouldn't do this, but I feel like nit-picking today. The TopicOpener obviously used python 3.x syntax. But The answers given by kiwi_steve and Brewer were in python 2.x syntax .
Luckily the TopicOpener didnt't get confused by that .

Yep. I use 2.7 and not 3, so I stuck with that syntax in the print statement to make sure it would run for me...

Brewer, on 03 July 2011 - 06:42 AM, said:

atraub, on 02 July 2011 - 10:53 AM, said:

Wow, I thought I'd be first to get to this one because it was posted so early in the morning

I have an email sent to me when a new thread is opened in the Python forum. I just happened to be online when the op posted their question.

And I live in New Zealand... which is GMT+12, so I get everything 15-odd hours before you

Re: How to write a word backwards

Posted 03 July 2011 - 05:02 AM

Wow, when I joined this forum, I never thought this many people would be helpful. Thank you so much for helping me out, I appreciate it. Also, even though I use python 3.x, i can still figure out the 2.x code as well (the book that I follow showed me how to do that ). Thanks for all the help.