获取 python 中两个特定字符之间的子字符串

Get substring between two spesific characters in python

我真的很困惑,无法编写为我执行此操作的代码。 看,这是我的字符串:

a="hello | my friends| in | stack | over | flow"

我想打印第一个和第二个“|”之间的 "my friends" 请帮助我

您可以使用 string.split 功能。

>>> a="hello | my friends| in | stack | over | flow"
>>> a.split('|')[1].strip()
'my friends'

a.split('|')[1] 从根据 |.

拆分输入创建的列表中打印索引 1 处的元素