Skip to content
Related Articles

Related Articles

Extract string from between quotations – Python
  • Last Updated : 03 Jan, 2021

In this article, we will learn to extract strings in between the quotations using Python.

Method 1:

To extract strings in between the quotations we can use findall() method from re library.

Python3




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

Output:

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

Method 2:



We can extract strings in between the quotations using split() method and slicing.

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);

Output:

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

Here you can learn more about Regular Expressions and list slicing in python.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :