【发布时间】:2019-06-29 08:38:52
【问题描述】:
我正在尝试删除句子中的前导昏迷,但我不明白为什么这不起作用
text = ",greetings friends"
text_l = text.split()
for word in text_l:
if word.startswith(','):
word = word[1:]
text = ' '.join(text_l)
>>> ,greetings friends
但确实如此。
text = ",greetings friends"
text_l = text.split()
for word in text_l:
if word.startswith(','):
indw = text_l.index(word)
text_l[indw] = word[1:]
text = ' '.join(text_l)
>>> greetings friends
【问题讨论】:
-
确实,您不需要拆分字符串。只需这样做:
if text.startswith[',']: text = text[1:]
标签: python python-3.x loops for-loop