title: Define a triple-quoted f-string with newline-containing substrings inside a function without outputted indents
body: <p>I'm trying to pretty-print a HTTP request (that I've mocked here).</p>
  
  <pre><code>from typing import NamedTuple
  
  class RequestMock(NamedTuple):
      method = 'POST'
      url = 'https://bob.com'
      body = 'body1\nbody2'
      headers = {'a': '1', 'b': '2'}
  </code></pre>
  
  <p>I have a function that does this:</p>
  
  <pre><code>req = RequestMock()
  
  def print1(req):
      headers = '\n'.join(f'{k}: {v}' for k, v in req.headers.items())
      s = '\n'.join([
          f'{req.method} {req.url}',
          headers,
          req.body
      ])
      print(s)
  
  print1(req)
  # POST https://bob.com
  # a: 1
  # b: 2
  # body1
  # body2
  </code></pre>
  
  <p>But when I've tried to rewrite it with <code>f-strings</code> for clarity and ease of modification, I get some bad indents:</p>
  
  <pre><code># what I want the code to look like
  def print2(req):
      headers = '\n'.join(f'{k}: {v}' for k, v in req.headers.items())
      s = f"""
      {req.method} {req.url}
      {headers}
      {req.body}
      """
      print(s)
  
  print2(req)
  #     POST https://bob.com
  #     a: 1
  # b: 2
  #     body1
  # body2
  </code></pre>
  
  <p>I know this is because I'm defining strings with newlines and putting them in a triple-quoted string. Is there a simple way to get the output I'm looking with a triple-quoted <code>f-string</code> defined in a function and without having to know the indentation level of its definition? I've played with <code>textwrap.indent</code>, <code>textwrap.dedent</code>, <code>str.lstrip</code>, <code>re</code>, etc., but the code stops being simple and pythonic fast. The closest thing I've come up with is the following, but the length is awkward and I feel like I'm repeating myself.</p>
  
  <pre><code>def print3(req):
      headers = '\n'.join(f'{k}: {v}' for k, v in req.headers.items())
      s = textwrap.dedent("""
      {method} {url}
      {headers}
      {body}
      """).strip()
      s = s.format(
          method=req.method,
          url=req.url,
          headers=headers,
          body=req.body,
      )
      print(s)
  print3(req)
  # POST https://bob.com
  # a: 1
  # b: 2
  # body1
  # body2
  </code></pre>