Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6154045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:11:44+00:00 2026-05-23T20:11:44+00:00

How do I escape a backslash and a single quote or double quote in

  • 0

How do I escape a backslash and a single quote or double quote in Python?

For example:

Long string = '''some 'long' string \' and \" some 'escaped' strings'''
value_to_change = re.compile(A EXPRESION TO REPRESENT \' and \")
modified = re.sub(value_to_change, 'thevalue', Long_string)

## Desired Output
modified = '''some 'long' string thevalue and thevalue some 'escaped' strings'''
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:11:45+00:00Added an answer on May 23, 2026 at 8:11 pm

    How you did it

    If your “long string” is read from a file (as you mention in a comment) then your question is misleading. Since you obviously don’t fully understand how escaping works, the question as you wrote it down probably is different from the question you really have.

    If these are the contents of your file (51 bytes as shown + maybe one or two end-of-line characters):

    some 'long' string \' and \" some 'escaped' strings
    

    then this is what it will look like in python:

    >>> s1 = open('data.txt', 'r').read().strip()
    >>> s1
    'some \'long\' string \\\' and \\" some \'escaped\' strings'
    >>> print s1
    some 'long' string \' and \" some 'escaped' strings
    

    What you wrote in the question will produce:

    >>> s2 = '''some 'long' string \' and \" some 'escaped' strings'''
    >>> s2
    'some \'long\' string \' and " some \'escaped\' strings'
    >>> print s2
    some 'long' string ' and " some 'escaped' strings
    >>> len(s)
    49
    

    Do you see the difference?

    There are no backslashes in s2 because they have special meaning when you use them to write down strings in Python. They have no special meaning when you read them from a file.

    If you want to write down a string that afterwards has a backslash in it, you have to protect the backslash you enter. You have to keep Python from thinking it has special meaning. You do that by escaping it – with a backslash.

    One way to do this is to use backslashes, but often the easier and less confusing way is to use raw strings:

    >>> s3 = r'''some 'long' string \' and \" some 'escaped' strings'''
    'some \'long\' string \\\' and \\" some \'escaped\' strings'
    >>> print s3
    some 'long' string \' and \" some 'escaped' strings
    >>> s1 == s3
    True
    

    How you meant it

    The above was only to show you that your question was confusing.

    The actual answer is a bit harder – when you are working with regular expressions, the backslash takes on yet another layer of special meaning. If you want to safely get a backslash through string escaping and through regex escaping to the actual regex, you have to write down multiple backslashes accordingly.

    Furthermore, rules for putting single quotes (') in single-quoted raw strings (r'') are a bit tricky as well, so I will use a raw string with triple single-quotes (r'''''').

    >>> print re.sub(r'''\\['"]''', 'thevalue', s1)
    some 'long' string thevalue and thevalue some 'escaped' strings
    

    The two backslashes stay two backslashes throughout string escaping and then become only one backslash without special meaning through regex escaping. In total, the regex says:
    “match one backslash followed by either a single-quote or a double-quote.”

    How it should be done

    Now for the pièce de résistance: The previous is really a good demonstration of what jwz meant1. If you forget about regex (and know about raw strings), the solution becomes much more obvious:

    >>> print s1.replace(r'\"', 'thevalue').replace(r"\'", 'thevalue')
    some 'long' string thevalue and thevalue some 'escaped' strings
    

    1 Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I build a regex example that can escape single quote and backslash
While searching on how to escape a single quote in String.Format, I found the
If I add a backslash+space to the start of double and single quoted strings,
I am trying to replace the backslash (escape) character in a Javascript string literal.
How would I replace a single quote (') with a backslash then single quote
I have a string that consists of some text \\computername.example.com\admin$. How would I do
Let's say I want to represent \q (or any other particular backslash-escaped character). That
How do I escape '<' and '>' character in sed. I have some xml
Do I need to escape backslash in PHP? echo 'Application\Models\User'; # Prints "Application\Models\User" echo
I'm trying to escape regex-reserved characters with a backslash (don't ask -- suffice it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.