简体   繁体   English

在Python中获取两个特殊字符之间的子字符串

[英]Get substring between two spesific characters in python

I am really confused that can't write a code that do this for me. 我真的很困惑,无法编写为我做到这一点的代码。 Look This is my string: 看,这是我的字符串:

a="hello | my friends| in | stack | over | flow" a =“ hello |我的朋友|在|堆栈|超过|流”

I want to print "my friends" which is between first and second "|" 我想打印介于第一和第二个“ |”之间的“我的朋友” Please help me 请帮我

You could use string.split function. 您可以使用string.split函数。

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

a.split('|')[1] prints the element at index 1 from the list which was created by splitting the input according to | a.split('|')[1]从列表中打印索引1处的元素,该列表是根据|分割输入而创建的 .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM