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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:35:21+00:00 2026-06-03T17:35:21+00:00

In my compilers class, I decided to write my compiler in Python since I

  • 0

In my compilers class, I decided to write my compiler in Python since I enjoy programming in Python, though I encountering an interesting issue with how characters are printed. The lexer I’m writing requires that strings containing the formfeed and backspace characters be printed to stdout in a very particular way: enclosed in double quotes, and printed as \f and \b, respectively. The closest I’ve gotten:

print("{0!r}".format("\b\f"))

which yields

'\x08\x0c'

Note the single quotes, and utf8 coding. The same command with two other characters I’m concerned with almost works:

print("{0!r}".format("\n\t"))

gives:

'\n\t'

To be clear, the result (including quotes) that I need to conform to the spec is

"\b\f"

Simple approaches like finding \b and \f and replacing them with “\b” and “\f” don’t seem to work…the “\” is just the way Python prints a backslash, so I can never seem to get just “\b\f” as one might expect.

Playing with various string encodings doesn’t seem to help. I’ve concluded that I need to write a custom string.Formatter, but I was wondering if there is another approach that I’d missed.

EDIT: Thanks for all the answers. I don’t think I did that good of a job asking the question though. The underlying issue is that I’m formatting the strings as raw because I want literal newlines to appear as “\n” and literal tabs to appear as “\t”. However, when I move to print the string using raw formatting, I lose the ability to print out “\b” and “\f” as all the answers below suggest.

I’ll confirm this tonight, but based on these answers, I think the approach I should be using is to format the output normally, and trap all the literal “\n”, “\t”, “\b”, and “\f” characters with escape sequences that will print them as needed. I’m still hoping to avoid using string.Formatter.

EDIT2: The final approach I’m going to use is to use non-raw string formatting. The non-abstracted version looks something like:

print('"{0!s}"'.format(a.replace("\b", "\\b").replace("\t", "\\t").replace("\f", "\\f").replace("\n","\\n")))
  • 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-03T17:35:34+00:00Added an answer on June 3, 2026 at 5:35 pm
    print("{0!r}".format("\b\f".replace("\b", "\\b").replace("\f", "\\f")))
    

    Or, more cleanly:

    def escape_bs_and_ff(s):
        return s.replace("\b", "\\b").replace("\f", "\\f")
    
    print("{0!r}".format(escape_bs_and_ff("\b\f"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a moment of madness, I decided to write a quadtree C++ template class.
I've been scratching my head since my first VHDL class and decided to post
In my graduate class on compiler construction we've been introduced to the concept of
I get compiler errors in : template<class _Other1, class _Other2> _Pair_base(_Other1&& _Val1, _Other2&& _Val2)
g++ compiler gives this error: expected `;' before 'it' template <typename T> class myList
After programming a little in C I decided to jump right into C++. At
I tend to write a class declaration in a source file and then decide
This compiles: class Ex1 { public int show() { try { int a=10/10; return
I developed a class that compiles to a dll and my friend uses this
The following source code compiles correctly with Visual Studio 2010: namespace NS { class

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.