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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:35:45+00:00 2026-06-02T19:35:45+00:00

i am very new to regular expression and trying get \ character using python

  • 0

i am very new to regular expression and trying get “\” character using python

normally i can escape “\” like this

print ("\\");
print ("i am \\nit");

output

\
i am \nit

but when i use the same in regX it didn’t work as i thought

print (re.findall(r'\\',"i am \\nit"));

and return me output

['\\']

can someone please explain why

  • 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-02T19:35:46+00:00Added an answer on June 2, 2026 at 7:35 pm

    EDIT: The problem is actually how print works with lists & strings. It prints the representation of the string, not the string itself, the representation of a string containing just a backslash is '\\'. So findall is actually finding the single backslash correctly, but print isn’t printing it as you’d expect. Try:

    >>> print(re.findall(r'\\',"i am \\nit")[0])
    \
    

    (The following is my original answer, it can be ignored (it’s entirely irrelevant), I’d misinterpreted the question initially. But it seems to have been upvoted a bit, so I’ll leave it here.)

    The r prefix on a string means the string is in “raw” mode, that is, \ are not treated as special characters (it doesn’t have anything to do with “regex”).

    However, r'\' doesn’t work, as you can’t end a raw string with a backslash, it’s stated in the docs:

    Even in a raw string, string quotes can be escaped with a backslash, but the backslash remains in the string; for example, r”\”” is a valid string literal consisting of two characters: a backslash and a double quote; r”\” is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).

    But you actually can use a non-raw string to get a single backslash: "\\".

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

Sidebar

Related Questions

I'm very new in regular expression and I'm trying to validate date using following
I am new to regular expression and this may be a very easy question
I have a problem using preg_match. (I'm very new to regular expressions) if(preg_match('^/http:\/\/myWebsite\.com\/', $_SERVER['HTTP_REFERER']))
I can't get my head around a solid RegEx for doing this, still very
I am very new to both Python and regular expressions, so bear with me.
Very new to Fitnesse, I need to write a test using regular expressions to
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
Very new to using Raphael.js I'm looking at the tutorials and I am able
Very new mobile developer here... I am trying to retrieve a list of tweets
I very new to Python, and fairly new to regex. (I have no Perl

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.