Naveen Vijay

Python 3’s new f string based string formatting is very powerful, simple and effective. It takes big readability hit when we try to compress the entire string output into a single line. I was wondering how do we do multilne f string without having to complex \t and stuff. I posted the same on Reddit and AlSweigart [Automate the Boring Stuff with Python fame] was kind enough to share his insights. I am resharing it here.

from uuid import uuid4

txt = f"""{uuid4()}
            is there a way to show this without the indent {uuid4()}
this works but need to give up on the readability"""

print(txt)

mapping over parameters

import textwrap

def someFunc():
    myName = 'Al'

    print(textwrap.dedent(f'''\
    Dear {myName},
    We regret to inform you that your
    comment has been downvoted.
      Sincerely,
      Reddit'''))

mapping over parameters

comments powered by Disqus