pythonspliterator

wants to devide a string in three part


string = "M/s Indian Tobacco Co. pvt. ltd., Godfrey Philips, VST Industries",

'''if starting word is M/s then it will devide it three part as end" ," character '''

like so
'''
[."M/s Indian Tobacco Co. pvt. ltd",
 "Godfrey Philips",
 "VST Industries"]'''

Solution

  • def process(string):
        if string.startswith("M/s"):
          return '\n'.join(f'{i}. {p.strip()}' for i, p in enumerate(string.split(','), start=1))
        else:
          # question does not specify what should happen
          return string
    
    print(process("M/s Indian Tobacco Co. pvt. ltd., Godfrey Philips, VST Industries"));
    

    Gives the output:

    1. M/s Indian Tobacco Co. pvt. ltd.
    2. Godfrey Philips
    3. VST Industries