Click here to Skip to main content
15,890,123 members
Articles / Programming Languages / Python

Replace All Spaces (Or Any Other Character) Between Quotes in Python

Rate me:
Please Sign up or sign in to vote.
4.40/5 (2 votes)
24 Jan 2015GPL31 min read 15.3K   2   5
Replace all spaces (or any other character) between quotes in Python

This is an oddly specific post, but I recently had this situation come up and solving it was actually not very straightforward. Specifically, I was writing a program to interpret commands and this program happened to split different arguments by spaces. So what if you have something like this:

FTP CD “This is a file path”\”that uses quotes”\”and was a pain in my bum”

Well, I needed to get rid of the spaces temporarily between just the stuff in quotes. Here’s how  I did it:

Python
# Use a regular expression to find any parts within quotes
string = "some string"\"which has quotes"\"and you don't want spaces or something"
for quoted_part in re.findall(r'\"(.+?)\"', string):
    # For each part that is within quotes replace the spaces with the pattern *#*#*#* or whatever you want
    string = string.replace(quoted_part, quoted_part.replace(" ", "*#*#*#*"))

Here's what the sample IO would look like:

Input going in: "some string"\"which has quotes"\"and you don't want spaces or something"

String after 1st iteration of the loop: "some*#*#*#*string"\"
which has quotes"\"and you don't want spaces or something"

2nd iteration: "some*#*#*#*string"\"which*#*#*#*has*#*#*#*quotes"\"
and you don't want spaces or something"

3rd iteration: "some*#*#*#*string"\"which*#*#*#*has*#*#*#*quotes"\"
and*#*#*#*you*#*#*#*don't*#*#*#*want*#*#*#*spaces*#*#*#*or*#*#*#*something"

Here’s How It Works

The Findall Method

In Python, the regular expression method findall method returns a list of all of the matched things. Here’s a good example I used from thegeekstuff:

>>> re.findall(r'dog', 'dog cat dog')
['dog', 'dog']
>>> re.findall(r'cat', 'dog cat dog')
['cat']

The Iterator

So what my code does is use the “for x in y” iterator construct to iterate over each instance of something found in quotes. For example, using my original command “This is a file path”\”that uses quotes”\”and was a pain in my bum” would have returned a list.

[‘This is a file path’, ‘that uses quotes’, ‘and was a pain in my bum’]

and the program will iterate over each of those things.

The Regular Expression

Here’s the regular expression I used: \”(.+?)\”

  • \”  means find a single quotation
  • . matches anything
  • + means for the preceding character, match anything with one or more instances (This basically means that there MUST be at least one thing between the quotes.)
  • ? in this context prevents the + from being too greedy

The Replace

Python
string= string.replace(quoted_part, quoted_part.replace(" ", "*#*#*#*"))

This part is fairly straightforward. It just finds the quoted part within the string and replaces the characters in it with whatever you want.

Hopefully this saves someone some time.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
United States United States
Grant is a specialist in computer security and networking. He holds a bachelors degree in Computer Science and Engineering from the Ohio State University. Certs: CCNA, CCNP, CCDA, CCDP, Sec+, and GCIH.

Comments and Discussions

 
QuestionNot an article Pin
Akhil Mittal1-Feb-15 19:44
professionalAkhil Mittal1-Feb-15 19:44 
QuestionRe: [My vote of 2] My low vote Pin
Grant Curell31-Jan-15 6:29
Grant Curell31-Jan-15 6:29 
Question[My vote of 2] My low vote Pin
Kirk 1038982126-Jan-15 8:41
Kirk 1038982126-Jan-15 8:41 
AnswerRe: [My vote of 2] My low vote Pin
Kenneth Haugland31-Jan-15 8:54
mvaKenneth Haugland31-Jan-15 8:54 
GeneralMy vote of 5 Pin
Volynsky Alex25-Jan-15 8:14
professionalVolynsky Alex25-Jan-15 8:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.