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 8447423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:10:37+00:00 2026-06-10T10:10:37+00:00

The output of the code below: rpl = ‘This is a nicely escaped newline

  • 0

The output of the code below:

rpl = 'This is a nicely escaped newline \\n'
my_string = 'I hope this apple is replaced with a nicely escaped string'
reg = re.compile('apple')
reg.sub( rpl, my_string )

..is:

'I hope this This is a nicely escaped newline \n is replaced with a nicely escaped string'

..so when printed:

I hope this This is a nicely escaped newline

is replaced with a nicely escaped string

So python is unescaping the string when it replaces ‘apple’ in the other string? For now I’ve just done

reg.sub( rpl.replace('\\','\\\\'), my_string )

Is this safe? Is there a way to stop Python from doing that?

  • 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-06-10T10:10:38+00:00Added an answer on June 10, 2026 at 10:10 am

    From help(re.sub) [emphasis mine]:

    sub(pattern, repl, string, count=0, flags=0)

    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl. repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.
    If it is
    a callable, it’s passed the match object and must return
    a replacement string to be used.

    One way to get around this is to pass a lambda:

    >>> reg.sub(rpl, my_string )
    'I hope this This is a nicely escaped newline \n is replaced with a nicely escaped string'
    >>> reg.sub(lambda x: rpl, my_string )
    'I hope this This is a nicely escaped newline \\n is replaced with a nicely escaped string'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

any idea how i can get the code below to produce this output? 1
The code below demonstrates this difference: #include <iostream> #include <string> int main() { char
The code below produces this output fine: This is page one. This is page
How do I get the code below to return the desired output. This is
I have this code below and I would like to display the output in
After running the code below I get this output: Eve 1200 Could anyone explain
Monotouch 5.0.2. Running the simple code below I get this in the output panel:
This code below best illustrates my problem: The output to the console (NB it
I have this code below. I don't know why inArray() doesn't output 0. Any
The code below wants to take a string and output only lowercase letters from

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.