#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    33
    Rep Power
    10

    typing a word backwords


    i am trying to print a word out backwords

    what i tried to do is get the last letter in a message the 'user'
    enters. Then, add it to a new word.and do that until every letter has been put into the new word.

    but i dont know how to 'get' the last letter of message.how do i do this?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11
    Code:
    >>> import string
    >>> l = string.letters
    >>> l
    'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    >>> len(l)
    52
    
    >>> newlist = [] //dump into a list type
    >>> for i in l:
    ... 	newlist.append(i)
    ... 	
    >>> newlist
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    
    newlist.reverse() //list's have a reverse function, runs inplace  ;)
    
    >>> newlist
    ['Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', 'N', 'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A', 'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
    
    >>> for i in newlist:
    ... 	print i,
    ... 	
    Z Y X W V U T S R Q P O N M L K J I H G F E D C B A z y x w v u t s r q p o n m l k j i h g f e d c b a
    you'll probably want to dump your list elements back into a string, so take a look at the string module and list module and play around.

    you mentioned also getting the last letter. You can use the slice operator [:]

    Code:
    >>> l[:52]
    'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    >>> l[len(l)-1:len(l)]  //l [51:52]
    'Z'
    >>>
    so you'll want to look up slice operators too

    hope that helps
    sf2k
    Last edited by sf2k; September 30th, 2005 at 10:56 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Originally Posted by megazear7
    i am trying to print a word out backwords
    Code:
    >>> word = "devshed"
    >>> word[::-1]
    'dehsved'
    what i tried to do is get the last letter in a message the 'user' enters. Then, add it to a new word.and do that until every letter has been put into the new word.
    Unless you have to, that's horrendously inefficient.

    Code:
    >>> word[::-1]
    
    is fast because it uses parts of Python that were written in C behind the scenes.
    
    >>> newword = ""
    >>> for char in reversed(word):
    ....      newword += char
    >>>
    
    Explains what you're doing
    
    >>> ''.join(list(reversed(word)))
    Is not at all fast, easy to read or efficient.
    but i dont know how to 'get' the last letter of message.how do i do this?
    Code:
    >>> "xyz"[-1]
    'z'
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    33
    Rep Power
    10
    ok, thanks ill try it

IMN logo majestic logo threadwatch logo seochat tools logo