python print without leading whitespace

49

python print without leading whitespace -

# Basic syntax:
from textwrap import dedent
print(dedent("""
	Text for which you want to remove
    leading whitespace when printed
    """))
# Note, IMO it's mostly useful for keeping your code nicely formatted
#	while still printing messages left-justified without leading whitespace

# Example usage:
# Say you have code like:
if __name__ == '__main__':
	if True:
    	print("""
            Make sure the user
            knows this.
            """)
# when you run your script, that text will be printed with the leading
# whitespace, which is gross.

# You could print it without whitespace by restructuring your code like:
if __name__ == '__main__':
	if True:
    	print("""
Make sure the user
knows this.
            """)
# but that screws up the readability of your code.

# Instead, with dedent you can do this and print without the leading whitespace
if __name__ == '__main__':
	if True:
    	print(dedent("""
			Make sure the user
			knows this.
            """))

Comments

Submit
0 Comments