4

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?

Fabio Capezzuoli
  • 599
  • 7
  • 23

2 Answers2

6

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)
TerryA
  • 58,805
  • 11
  • 114
  • 143
4
print('The lucky number is:', num, sep='\n')

Output:

The lucky number is
888
Inon Peled
  • 691
  • 4
  • 11