13 Replies - 11080 Views - Last Post: 04 July 2011 - 06:21 PM Rate Topic: -----

#1 pvpkiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-June 11

How to write a word backwards

Posted 02 July 2011 - 05:01 AM

I am trying to write a program, where when a user enters a word, it writes it backwards. Please help me figure it out.
word = input("Enter a word: ")
count = len(word)
print("\nThere are", count, "words.")
    print(word[count - 1])
    print(word[count - 2])
    print(word[count - 3])
    print(word[count - 4])
    print(word[count - 5])
    print(word[count - 6])
    print(word[count - 7])



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.

This post has been edited by pvpkiller: 02 July 2011 - 05:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to write a word backwards

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: How to write a word backwards

Posted 02 July 2011 - 05:09 AM

The best way to do this is to use extended slice syntax.

word = raw_input("Enter a word: ")
print word, "in reverse is", word[::-1]


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.

Make sense?
Was This Post Helpful? 3
  • +
  • -

#3 pvpkiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-June 11

Re: How to write a word backwards

Posted 02 July 2011 - 05:16 AM

Yes, it makes sense. Thank you very much. Oh and i am new to python, only been studying it for 3 days, but i did learn slicing. Thank you for ur help :)
Was This Post Helpful? 0
  • +
  • -

#4 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

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:

word = raw_input("Enter a word: ")
count = len(word)
print "\nThere are", count, "letters."
for i in range(count):
    print word[count - i - 1],


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 :bigsmile: )

This post has been edited by kiwi_steve: 02 July 2011 - 05:58 AM

Was This Post Helpful? 2
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: How to write a word backwards

Posted 02 July 2011 - 06:23 AM

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.

This post has been edited by atraub: 02 July 2011 - 06:32 AM

Was This Post Helpful? 2
  • +
  • -

#6 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

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 :detective: .
Luckily the TopicOpener didnt't get confused by that :D .

This post has been edited by atraub: 02 July 2011 - 09:27 AM

Was This Post Helpful? 2
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: How to write a word backwards

Posted 02 July 2011 - 09:26 AM

That is a tad bit nit picky... but I suppose it was worth pointing out. Good catch.
Was This Post Helpful? 1
  • +
  • -

#8 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: How to write a word backwards

Posted 02 July 2011 - 10:42 AM

View Postatraub, 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.
Was This Post Helpful? 1
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: How to write a word backwards

Posted 02 July 2011 - 11:48 AM

I get the same email... I am the forum leader after all ;)
Was This Post Helpful? 2
  • +
  • -

#10 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: How to write a word backwards

Posted 02 July 2011 - 11:04 PM

View PostNallo, 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 :detective: .
Luckily the TopicOpener didnt't get confused by that :D .


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...

View PostBrewer, on 03 July 2011 - 06:42 AM, said:

View Postatraub, 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 :bananaman:
Was This Post Helpful? 1
  • +
  • -

#11 pvpkiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-June 11

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 :P). Thanks for all the help. :)
Was This Post Helpful? 0
  • +
  • -

#12 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: How to write a word backwards

Posted 03 July 2011 - 05:16 AM

View Postpvpkiller, on 04 July 2011 - 01:02 AM, said:

Thanks for all the help. :)


You're very welcome :)
Was This Post Helpful? 0
  • +
  • -

#13 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: How to write a word backwards

Posted 03 July 2011 - 10:10 AM

View Postpvpkiller, on 03 July 2011 - 09:32 AM, said:

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 :P). Thanks for all the help. :)


Just out of curiosity, what book would that be? I own a copy of Mark Lutz's Learning Python and that covers both 2.6 (I think) and 3.0.
Was This Post Helpful? 0
  • +
  • -

#14 pvpkiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-June 11

Re: How to write a word backwards

Posted 04 July 2011 - 06:21 PM

View PostBrewer, on 03 July 2011 - 10:10 AM, said:

View Postpvpkiller, on 03 July 2011 - 09:32 AM, said:

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 :P). Thanks for all the help. :)


Just out of curiosity, what book would that be? I own a copy of Mark Lutz's Learning Python and that covers both 2.6 (I think) and 3.0.


I use "Python 3 for absolute beginners" by Tim Hall and J-P Stacey and "Python programming for absolute beginner(3rd edition) by Michael Dawson.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1