Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Replace a string starting from a charecter

I need an optimised way to replace all trailing characters starting from a '/' in a string.

For example:

mytext = "this is my/string"

i want a result like this

mytext = "this is my/text"

only the string after '/' must be replaced and must be done in optimised way. can anyone find a solution for me?

like image 775
upv Avatar asked Sep 19 '25 06:09

upv


1 Answers

I'm not sure what you mean by optimized but i'd do:

>>> import re
>>> mytext = "this is my/string"
>>> re.sub('/.*','/text',mytext)
'this is my/text'
like image 55
Chris Seymour Avatar answered Sep 20 '25 20:09

Chris Seymour