Sunday, October 26, 2025
HomeLanguagesExtract string from between quotations – Python

Extract string from between quotations – Python

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 "Lazyroar" "for" "Lazyroar" '
 
print(re.findall('"([^"]*)"', inputstring))


Output:

['Lazyroar', 'for', 'Lazyroar']

Method 2:

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

Python3




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


Output:

['Lazyroar', 'for', 'Lazyroar']

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 "Lazyroar" "for" "Lazyroar" '
result = inputstring.split()
res=[]
for i in result:
    if(i.startswith('"') and i.endswith('"')):
        i=i.replace('"',"")
        res.append(i)
print(res)


Output

['Lazyroar', 'for', 'Lazyroar']
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS