Python Split String Double Quotes

✨Summary: The two methods to split the string at double quotes are:
(i) given_string.split('"')
(ii) re.split('"', given_string)

Minimal Example

import re
text = 'Good "Better" Best'
print(text.split('"'))
print(re.split('"', text))

# OUTPUT: ['Good, ', 'Better', ', Best']

Problem Formulation

πŸ“œProblem: Given a string. How will you split the string using the double quotes as the delimiter?

Example

Let’s visualize the problem with the help of an example:

# Input 
text = 'abc"xyz"lmn"qpr"'
# Expected Output
['abc', 'xyz', 'lmn', 'qpr']

Now that we have an overview of our problem let us dive into the solutions without further ado.

Method 1: Using split()

The split('sep') function splits the given string at a given separator and returns a split list of substrings. It returns a list of the words in the string, using sep as the delimiter.

Approach: We can simply split the string at the double quotes by using the double quotes as the delimiter within the split function. However, after split() returns the resultant list, you will get a list of substrings with an empty string at the end. To remove that empty string, you can use the pop() method. The list.pop() method removes the last element from an existing list.Β 

Code:

text = 'abc"xyz"lmn"qpr"'
res = text.split('"')
res.pop()
print(res)

# ['abc', 'xyz', 'lmn', 'qpr']

🌎Related Read: Python List pop()

Method 2: Using re.split

The re.split(pattern, string) method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example, re.split('a', 'bbabbbab') results in the list of strings ['bb', 'bbb', 'b'].

Approach: Use the split() method from the regex package and feed in double quotes as the pattern, i.e., the first argument and feed in the given string as the sequence, i.e., second argument.

Code:

import re

text = 'abc"xyz"lmn"qpr"'
x = re.split('"', text)
x.pop()
print(x)

# ['abc', 'xyz', 'lmn', 'qpr']

🌎Related Read: Python Regex Split

Bonus Read

πŸš€Python | Split String by Space and Preserve Quoted Strings

Now we will not only split the string using double quotes as the delimiter but also preserve the quotations. To do this, you can use a module known as shlex and use its split method. Simply, pass the string to the shlex.split() function which will then split the string using a shell like syntax.

source: Official documentation

Let’s visualize the problem with the help of an example:

# Input 
text =  'one "two" three "four"'
# Expected output 
['one', '"two"', 'three', '"four"']

Approach: We have to use the split method from the shlex module to split the string. The method also removes the leading or trailing spaces while splitting the string. The first argument passed is the given string and the second argument is the posix value. When the posix argument is set to False, it preserves the double quotes of the string. Remember that by default, the posix argument is set to True.

Code:

import shlex
text = 'one "two" three "four"'
res = shlex.split(text, posix = False)
print(res)

#['one', '"two"', 'three', '"four"']

πŸš€Extract String from between Double Quotes

Previously we learned how to split a string using double quotes as the delimiter. Now we will learn how you can extract only the substring which are present between double quotes.

Problem

# Input
text = ''abc,"xyz",hlmni,"qpr"'
# Output
['xyz', 'qpr']

Solution: You can use the expression re.findall('"([^"]*)"' to find all the substrings that lie between double quotes. Here, findall is a method of the regex package that returns a list containing all matches.

Code:

import re

text = 'abc,"xyz",hlmni,"qpr"'
print(re.findall('"([^"]*)"', text))

Pattern Explanation:
[^"]* – matches and includes all the characters that start with double quotes
() – used to capture the groups of characters lying between the quotations

Conclusion

Therefore, in this article, we discussed two methods- split() and re.split() to split a string at the double quotes. We also had a look at the solutions to some other variations of the given problem. I hope you enjoyed this article and it helped you to enhance your coding skills. Please subscribe and stay tuned for more interesting articles!


Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.