Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python output: how to remove indent of a new line?

Tags:

python

output

There's an annoying little problem with Python print() output: the first line after \n is indented. Like this:

num = 888
print("The lucky number is:\n", num)

The lucky number is:
 888

I'd really like to have '888' aligned left. How can I do it?

like image 351
Fabio Capezzuoli Avatar asked Oct 23 '25 10:10

Fabio Capezzuoli


1 Answers

You can use string formatting to put num wherever you want in the string:

print(f"The lucky number is:\n{num}")

If f-strings aren't available to use, you can also use str.format():

print("The lucky number is:\n{}".format(num))

And finally, while I don't recommend it because it's deprecated, you can use percentage symbol formatting:

print("The lucky number is:\n%d" % num)
like image 196
TerryA Avatar answered Oct 25 '25 22:10

TerryA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!