Thread: backward

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

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11

    backward


    any shorter ways to do this?

    Code:
    word = raw_input("A word: ")
    backward = ""
    for n in word:
        word_minus_last_letter = word[0:-1]
        last = word[-1]
        word = word_minus_last_letter
        backward =  backward + last
    print "Backward:", backward
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Code:
    word = raw_input("A word: ")
    print "Backward:", word[::-1]
    yes
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11
    wow cool! what exactly does :: mean generally?
    thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    From Python Quick Reference

    s[i: j: step] ===> Slice of s from i (included) to j(excluded). Optional step value, possibly negative (default: 1).

    (1) if i or j is negative, the index is relative to the end of the string, ie len(s)+i or len(s)+j is substituted. But note that -0 is still 0.

    (2) The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j.
    If i or j is greater than len(s), use len(s). If i is omitted, use len(s). If i is greater than or equal to j, the slice is empty.

    Some examples for a good grasp......

    Even numbers
    Code:
    >>> num=range(20)
    >>> num[::2]
    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

    Negative value of step could be used to make a copy of the list in a reverse order
    Code:
    >>> num[::-1]
    [19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    >>> name="DeeDee"
    >>> name[::-1]
    'eeDeeD'
    >>>
    This extended slicing works for strings, tuples and arrays too!

    Subha
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11
    nice thanks subha
    that saves one for lots of unecessary lines
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    3
    Rep Power
    0

    string.reverse()


    #And there is also:
    import string
    word=raw_input('give me a word: ')
    print word.reverse()
    #Tony (untested)

    Originally Posted by Dag
    nice thanks subha
    that saves one for lots of unecessary lines
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Code:
    #And there is also:
    import string
    word=raw_input('give me a word: ')
    print word.reverse()
    #Tony (untested)
    Code:
    >>> import string
    >>> word = "hi"
    >>> word.reverse()
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
    AttributeError: 'str' object has no attribute 'reverse'

  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    3
    Rep Power
    0

    backward


    sorry, reverse is a list method. This works, butnot as flexible as the posted method:

    #And there is also:
    word=raw_input('give me a word: ')
    word=list(word)
    word.reverse()
    word2=''
    for item in word:
    word2=word2+item
    print word2
    #Tony (tested)

    Originally Posted by sfb
    Code:
    >>> import string
    >>> word = "hi"
    >>> word.reverse()
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
    AttributeError: 'str' object has no attribute 'reverse'

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    PHP Code:
    >>> word=list(raw_input('give me a word'))
    >>> 
    word
    ['e''n''o''d']
    >>> 
    word.reverse()
    >>> 
    ''.join(word)
    'done'
    >>> 

    Subha

IMN logo majestic logo threadwatch logo seochat tools logo