想把一个字符串分成三部分
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"]'''
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"));
给出输出:
1. M/s Indian Tobacco Co. pvt. ltd.
2. Godfrey Philips
3. VST Industries
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"]'''
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"));
给出输出:
1. M/s Indian Tobacco Co. pvt. ltd.
2. Godfrey Philips
3. VST Industries