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

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    Why doesn’t the slice’s step index work with ‘replace’?


    Here’s my code:
    >>> a = ‘Hello Bello Jello!’
    >>> a = a.replace(a[::2],’H’)
    >>> a
    'Hello Bello Jello!'
    >>> a = a.replace(a[0:2],'H')
    >>> a
    'Hllo Bello Jello!'
    While using the ‘replace’ command; without the step index in slice, it works but when I include the step index, it doesn't and returns the previously assigned value of ‘a’. Why is that?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    439
    Rep Power
    67
    Originally Posted by Akshat1
    While using the ‘replace’ command; without the step index in slice, it works but when I include the step index, it doesn't and returns the previously assigned value of ‘a’. Why is that?
    Simple. a[::2] equals to “HloBloJlo” in your example: every second letter in the string “a”. When you do:

    Code:
    a = a.replace(a[::2], 'H')
    you are asking to replace the string “HloBloJlo” with “H” – but since “HloBloJlo” doesn’t appear in a, it cannot be replaced! So the string is returned intact.

    OTOH, in

    Code:
    a = a.replace(a[0:2], 'H')
    you are asking to replace “He” (= a[0:2]) with “H”, and this string is a substring of “a” and can be replaced.
    My armada: openSUSE 13.2 (home desktop, work desktop), openSUSE 13.1 (home laptop), Debian GNU/Linux 7.7.0 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481

    Solution to the step replace


    Code:
    >>> a = 'Hello Bello Jello!'
    >>> la = list(a)
    >>> la
    ['H', 'e', 'l', 'l', 'o', ' ', 'B', 'e', 'l', 'l', 'o', ' ', 'J', 'e', 'l', 'l', 'o', '!']
    >>> la[::2] = 'H'*(len(la[::2]))
    >>> la
    ['H', 'e', 'H', 'l', 'H', ' ', 'H', 'e', 'H', 'l', 'H', ' ', 'H', 'e', 'H', 'l', 'H', '!']
    >>> ''.join(la)
    'HeHlH HeHlH HeHlH!'
    >>>
    Last edited by b49P23TIvg; February 11th, 2013 at 11:33 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by SuperOscar
    Simple. a[::2] equals to “HloBloJlo” in your example: every second letter in the string “a”. When you do:

    Code:
    a = a.replace(a[::2], 'H')
    you are asking to replace the string “HloBloJlo” with “H” – but since “HloBloJlo” doesn’t appear in a, it cannot be replaced! So the string is returned intact.

    OTOH, in

    Code:
    a = a.replace(a[0:2], 'H')
    you are asking to replace “He” (= a[0:2]) with “H”, and this string is a substring of “a” and can be replaced.
    Thank you soooo much. Thanks

IMN logo majestic logo threadwatch logo seochat tools logo