Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning to string slice
#1
Is this the best practice for assigning to a string slice? I realized in that case I could have used replace(), but I was thinking of the general case, e.g. I have located a pattern in the string.

>>> sf='23'
>>> s='1234'
>>> i=s.find( sf)
>>> s[:i] + s[i+len(sf)]
'14'
Reply
#2
I'd use replace:
>>>s = s.replace('23', '')
>>> s
'14'
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fix pandas copy/slice warning. deanhystad 3 757 Sep-07-2023, 03:18 PM
Last Post: deanhystad
  Slice creates new objects? fmr300 4 1,268 Jul-20-2022, 12:34 PM
Last Post: fmr300
  InvalidIndexError: (slice(None, None, None), slice(None, -1, None)) SuperNinja3I3 1 4,286 Jul-15-2022, 05:59 AM
Last Post: Larz60+
  Assigning a new value to variable uriel 1 1,571 Dec-04-2021, 02:59 PM
Last Post: Underscore
  Slice list Moris526 1 1,618 Dec-24-2020, 02:19 AM
Last Post: deanhystad
  increase and decrease a slice value? KEYS 2 2,055 Nov-10-2020, 11:35 PM
Last Post: KEYS
  assigning a variable :( gr3yali3n 0 1,289 Sep-22-2020, 09:02 PM
Last Post: gr3yali3n
  Assigning variables Godserena 4 2,135 Apr-26-2020, 06:59 AM
Last Post: buran
  Assigning a Variable Help MC2020 5 2,880 Jan-06-2020, 10:54 PM
Last Post: MC2020
  Pass Tuple as a Slice nagymusic 2 2,318 Dec-12-2019, 04:42 AM
Last Post: nagymusic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020