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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:25:51+00:00 2026-05-27T07:25:51+00:00

How do I indent a multiline string in the context of string formatting? e.g.

  • 0

How do I indent a multiline string in the context of string formatting? e.g.

'''
<
     %s
>
''' % (paragraph)

where paragraph contains newlines. (‘foo\nbar’)

If I use the above code, I get output like this:

'''
<
    foo
bar
>
'''

when I really want this:

'''
<
    foo
    bar
>
'''

I know I could do something like:

'''
<
%s
>
''' % (paragraph)

but this breaks readability for my purposes.

I also realize I could just write some code to indent all but the first line by 1 indent, but this isn’t really an extensible solution (what if I have 2 indents? or 3? etc.)

EDIT:
Before you post an answer, consider how your solution works with something like this:

'''
<
    %s
    <
        %s
        %s
        <
            %s
        >
    >
>
''' % (p1, p2, p3, p4)
  • 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-27T07:25:52+00:00Added an answer on May 27, 2026 at 7:25 am

    Okay, the simple doesn’t suit your purposes. Either you need to use a document object model and a flattening system with appropriate indentation.

    Or, you could do something fairly hacky to detect the amount of whitespace before each substitution. This is pretty fragile:

    import re
    
    def ind_sbt(text,indent='\t'):
        return text.replace('\n','\n%s' % (indent,))
    
    def adjust_indents(fmtstr,*args):
        """Hacky, crude detection of identation level"""
        indented_substitutions = re.compile(r'\n(\s+)%s')
        return fmtstr % tuple([ ind_sbt(arg,indent=i) for (arg,i) in zip(args,indented_substitutions.findall(fmtstr))])
    
    p1 = p2 = p3 = p4 = "foo\nbar"
    
    print adjust_indents('''
    <
        %s
        <
            %s
            %s
            <
                %s
            >
        >
    >
    ''',p1,p2,p3,p4)
    

    Yielding:

    <
            foo
            bar
            <
                    foo
                    bar
                    foo
                    bar
                    <
                            foo
                            bar
                    >
            >
    >
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Visual Studio keeps trying to indent the code inside namespaces. For example: namespace Foo
Is there an automatic way to output full indent code using a MVC solution?
Is there a free XML formatting (indent) tool available where I can past an
When i indent code in a mailer-view, i see the indentation in the sent
How to make Emacs to indent cases like this switch ($foo) { case foo:
I usually use 4 white spaces to indent C programs, but in order to
I am trying to use indent program to indent C++ programs. But it does
Duplicate: How to indent code in vim editor in Windows? Tabbing selected section in
I want to indent the following piece of code. How would a lisper indent
For generated code I have an option to either indent or or not indent

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.