Reputation: 1130
This substring related question appears to never mention another potential goal when slicing strings: obtaining what's left after a slice.
Is there a way to get what's left over when performing a slice operation without two separate steps where you join what's left over?
This would be brute force way, but it uses two slice operations and a join.
myString = "how now brown trow?"
myString[:4] + myString[-5:]
>>> 'how trow?'
Can this be done using the slicing notation without making two slices and joining them together?
Upvotes: 2
Views: 323
Reputation: 180401
If the slice is unique that you want to remove you could str.replace
:
myString = "how now brown trow?"
s = myString.replace(myString[4:-5],"")
print(s)
how trow?
Upvotes: 2
Reputation: 251373
No. You can't get non-contiguous pieces with a single slice operation.
Upvotes: 2