How to output print with spaces?

#! python3
print ("Space            wanted")

gives: Space wanted
How to print the embedded spaces?

The output console is html/css…
So:

#! python3
print("Spaces&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspend")
#or
spaces = "&nbsp" * 20
print("Spaces" + spaces + "end")
#or
print("Spaces" + "&nbsp" * 20 + "end")

The last is more concise. Or you could add a function.
Or use replace.

y = "Spaces              end"
y = y.replace(" ", "&nbsp")
print(y)

Thank you!
I now can use:

#! python3
# -*- coding: utf-8 -*-
x = "space     23.45"
y = x.replace(" ", "&nbsp")
print (y)
x = "space".ljust(10) + "23.45".rjust(8)
y = x.replace(" ", "&nbsp")
print (y)
x = f"{'space': <10}{"23.45": >8}"
y = x.replace(" ", "&nbsp")
print (y)

giving:

space     23.45
space        23.45
space        23.45

You may want to look at other html/css depending on how you want to format your output.

1 Like