[Tutor] Slicing index
Mark Lawrence
breamoreboy at gmail.com
Sun Oct 18 14:31:19 EDT 2020
On 18/10/2020 13:57, Carlene Wong via Tutor wrote:
> Hello, I’m new but I’m getting trouble with slicing an index into another.
> Eg. Def mysentence (sentence, old, new)
> If old in sentence[-4:]:
> n= sentence.index(old)
> newsent=Sentence [:n]+new
> return newsent
> return mysentence
> print(mysentence(“Hello there, it’s raining cats and cats”, “ cats”, “ dogs“)
>
> It keeps printing
> Hello there, it’s raining dogs
No it doesn't, you'd fail at the 'Def' with an invalid syntax error and
there's a colon missing at the end of the line. Having said that
looking at what you're trying to achieve why bother with string slicing,
why not the string 'replace' method
https://docs.python.org/3/library/stdtypes.html#str.replace ?
>
> Your help
>
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list