The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Why doesn’t the slice’s step index work with ‘replace’?
Discuss Why doesn’t the slice’s step index work with ‘replace’? in the Python Programming forum on Dev Shed. Why doesn’t the slice’s step index work with ‘replace’? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 11th, 2013, 03:09 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 27
Time spent in forums: 5 h 39 m 6 sec
Reputation Power: 0
|
|
|
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?
|

February 11th, 2013, 04:55 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | 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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

February 11th, 2013, 10:31 AM
|
 |
Contributing User
|
|
|
|
|
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!'
>>>
__________________
[code] Code tags[/code] are essential for python code!
Last edited by b49P23TIvg : February 11th, 2013 at 10:33 AM.
|

February 14th, 2013, 07:58 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 27
Time spent in forums: 5 h 39 m 6 sec
Reputation Power: 0
|
|
Quote: | 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 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|