Shubhankar_Goswami Shubhankar_Goswami - 3 months ago 24
Python Question

Strip part of string before a particular character, in a case where the character occurs multiple times

I need to strip the string off the part that occurs before the character

, where
may occur multiple times.
For Example:

input: 'Mark: I am sending the file: abc.txt'
output: 'I am sending the file: abc.txt'

The function I have is this (Python code)

def process_str(in_str):
str_list = in_str.split(':')[1:]
out_str = ''
for each in str_list:
out_str += each
return out_str

The output I am getting is
'I am sending the file abc.txt'
without the second
Is there a way to correct this?
Also can this code be made more efficient in time and space complexity?


split isn't the best approach for this. You want to use a regular expression.

import re

def process_str(in_str):
  return re.sub('^.*?: ', '', in_str)

This returns the string without anything up to the first : (colon followed by space). You can read more about Python regular expressions here.