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

  • Home
  • SEARCH
  • 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 561447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:27:03+00:00 2026-05-13T12:27:03+00:00

I have a string where special characters like ‘ or or & (…) can

  • 0

I have a string where special characters like ' or " or & (…) can appear. In the string:

string = """ Hello "XYZ" this 'is' a test & so on """

how can I automatically escape every special character, so that I get this:

string = " Hello "XYZ" this 'is' a test & so on "
  • 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-13T12:27:04+00:00Added an answer on May 13, 2026 at 12:27 pm

    In Python 3.2, you could use the html.escape function, e.g.

    >>> string = """ Hello "XYZ" this 'is' a test & so on """
    >>> import html
    >>> html.escape(string)
    ' Hello "XYZ" this 'is' a test & so on '
    

    For earlier versions of Python, check http://wiki.python.org/moin/EscapingHtml:

    The cgi module that comes with Python has an escape() function:

    import cgi
    
    s = cgi.escape( """& < >""" )   # s = "&amp; &lt; &gt;"
    

    However, it doesn’t escape characters beyond &, <, and >. If it is used as cgi.escape(string_to_escape, quote=True), it also escapes ".


    Here’s a small snippet that will let you escape quotes and apostrophes as well:

     html_escape_table = {
         "&": "&amp;",
         '"': "&quot;",
         "'": "&apos;",
         ">": "&gt;",
         "<": "&lt;",
         }
    
     def html_escape(text):
         """Produce entities within text."""
         return "".join(html_escape_table.get(c,c) for c in text)
    

    You can also use escape() from xml.sax.saxutils to escape html. This function should execute faster. The unescape() function of the same module can be passed the same arguments to decode a string.

    from xml.sax.saxutils import escape, unescape
    # escape() and unescape() takes care of &, < and >.
    html_escape_table = {
        '"': "&quot;",
        "'": "&apos;"
    }
    html_unescape_table = {v:k for k, v in html_escape_table.items()}
    
    def html_escape(text):
        return escape(text, html_escape_table)
    
    def html_unescape(text):
        return unescape(text, html_unescape_table)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need a little help with string formatting... I have a string like this: Bmw
I have URLs with some special characters and i would like to replace them
Possible Duplicate: How to find whether a string contains any of the special characters?
Usually when my regex patterns look like this: http://www.microsoft.com/ Then i have to escape
I have a string which I want to use in a regular expression it
I have print $str; abcd*%1234$sdfsd..#d The string would always have only one continuous stretch
I am using the official JSON library for my java project ad i have
I have the following stringBuilder that I am using as part of Log4net log
I need to find, process and remove (one by one) any substrings that match
I'm using javascript to request a image generated by the server in real time.

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.