Extract string from between quotations – Python
Last Updated :
16 Feb, 2023
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" '
result = inputstring.split( '"' )[ 1 :: 2 ]
print (result);
|
Output:
['geeks', 'for', 'geeks']
Time Complexity: O(n)
Auxiliary Space: O(1)
Here you can learn more about Regular Expressions and list slicing in python.
Method 3: Using startswith(),endswith() and replace() methods
Python3
inputstring = 'some strings are present in between "geeks" "for" "geeks" '
result = inputstring.split()
res = []
for i in result:
if (i.startswith( )):
i = i.replace( '"' ,"")
res.append(i)
print (res)
|
Output
['geeks', 'for', 'geeks']
Share your thoughts in the comments
Please Login to comment...