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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:52:06+00:00 2026-05-30T00:52:06+00:00

Say I have a string in Python 3.2 like this: ‘\n’ When I print()

  • 0

Say I have a string in Python 3.2 like this:

'\n'

When I print() it to the console, it shows as a new line, obviously. What I want is to be able to print it literally as a backslash followed by an n. Further, I need to do this for all escaped characters, such as \t. So I’m looking for a function unescape() that, for the general case, would work as follows:

>>> s = '\n\t'
>>> print(unescape(s)) 
'\\n\\t'

Is this possible in Python without constructing a dictionary of escaped characters to their literal replacements?

(In case anyone is interested, the reason I am doing this is because I need to pass the string to an external program on the command line. This program understands all the standard escape sequences.)

  • 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-30T00:52:08+00:00Added an answer on May 30, 2026 at 12:52 am

    To prevent special treatment of \ in a literal string you could use r prefix:

    s = r'\n'
    print(s)
    # -> \n
    

    If you have a string that contains a newline symbol (ord(s) == 10) and you would like to convert it to a form suitable as a Python literal:

    s = '\n'
    s = s.encode('unicode-escape').decode()
    print(s)
    # -> \n
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a string that looks like this: str = The &yquick &cbrown
So let's say we have a string that is like this: ‰û]M§Äq¸ºþe Ø·¦ŸßÛµÖ˜eÆÈym™ÎB+KºªXv©+Å+óS—¶ê'å‚4ŒBFJF󒉚Ү}Fó†ŽxöÒ&‹¢ T†^¤(
say I have a string like so Testing How could I have it remove
Lets say I have a string COLIN. The numeric value of this string would
So say I have a string like so of a path $path = '/path/to/../../up/something.txt';
Lets say a have a string such as this one: string txt = Lore
Say I have a text file formatted like this: 100 20 the birds are
For example, say I have string like: duck duck duck duck goose goose goose
HI, lets say I have a Java interface B, something like this. B.java :
Let's say I have a string 'gfgfdAAA1234ZZZuijjk' and I want to extract just the

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.