📜  从引号之间提取字符串– Python

📅  最后修改于: 2022-05-13 01:55:22.002000             🧑  作者: Mango

从引号之间提取字符串– Python

在本文中,我们将学习使用Python提取引号之间的字符串。

方法一:

要提取引号之间的字符串,我们可以使用re库中的 findall() 方法。

Python3
import re
inputstring = ' some strings are present in between "geeks" "for" "geeks" '
  
print(re.findall('"([^"]*)"', inputstring))


Python3
inputstring = 'some strings are present in between "geeks" "for" "geeks" '
    
"""
here split() method will split the string for every quotation ( " ) .i.e.
['some strings are present in between ', 'geeks', ' ', 'for', ' ', 'geeks', ' ']. 
  
Then we will be storing all the strings at odd index.
  
"""
  
result = inputstring.split('"')[1::2]
print(result);


输出:

['geeks', 'for', 'geeks']

方法二:

我们可以使用 split() 方法和切片提取引号之间的字符串。

蟒蛇3

inputstring = 'some strings are present in between "geeks" "for" "geeks" '
    
"""
here split() method will split the string for every quotation ( " ) .i.e.
['some strings are present in between ', 'geeks', ' ', 'for', ' ', 'geeks', ' ']. 
  
Then we will be storing all the strings at odd index.
  
"""
  
result = inputstring.split('"')[1::2]
print(result);

输出:

['geeks', 'for', 'geeks']

在这里你可以了解更多关于正则表达式和Python中的列表切片。