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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:57:21+00:00 2026-05-17T16:57:21+00:00

I’d like to do something equivalent to Python’s repr : >>> x = a\nb\nc

  • 0

I’d like to do something equivalent to Python’s repr:

>>> x = "a\nb\nc"

>>> print x
a
b
c
>>> repr(x)
"'a\\nb\\nc'"

>>> print repr(x)
'a\nb\nc'

How can I do that in D? Is there a format directive similar to Python’s %r?

EDIT: I want to be able to print strings in their escaped form for debugging purposes (I’ve just started to learn D and I am writing silly text processing functions).

  • 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-17T16:57:21+00:00Added an answer on May 17, 2026 at 4:57 pm

    In D strings are not objects and when you use escape characters you are actually storing the character and not the escape itself (which I would expect Python was doing too). So if you want to convert a string into its escaped form you would need to modify the output yourself (I don’t know of any library function for this).

    Otherwise Objects can override the toString() function which should produce a string representation of the object.

    Update: Here is an example of repr in D. But note, since the original text is not stored anywhere in the executable it will convert literal representations too. There shouldn’t be an issue with Unicode as the normal integrity of string is intact.

    import std.stdio;
    import std.exception;
    
    void main() {
        writeln(repr("This\nis\n\t*My\v Test"));
    }
    
    string repr(string s) {
        char[] p;
        for (size_t i; i < s.length; i++)
        {
            switch(s[i]) {
                case '\'':
                case '\"':
                case '\?':
                case '\\':
                    p ~= "\\";
                    p ~= s[i];
                    break;
                case '\a':
                    p ~= "\\a";
                    break;
                case '\b':
                    p ~= "\\b";
                    break;
                case '\f':
                    p ~= "\\f";
                    break;
                case '\n':
                    p ~= "\\n";
                    break;
                case '\r':
                    p ~= "\\r";
                    break;
                case '\t':
                    p ~= "\\t";
                    break;
                case '\v':
                    p ~= "\\v";
                    break;
                default:
                    p ~= s[i];
                    break;
            }
        }
    
        return assumeUnique(p);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.