How to remove extra space

Why there is extra spaces (marked yellow) before dot and how to get rid of them?

x = float(input(“Give a decimal number: “))
y = int(input(“Give an integer number: “))
result = x**y
print(x,“to the power of”,y,“is approximately”,(round(result)),”.”)
print(“To be exact it is”,(round(result, 2)),”.”)

image

print, by default, prints a space between each argument it prints:

>>> print('x', 'y')
x y

The solution is to use string formatting:

print("{} to the power of {} is approximately {}.".format(x, y, round(result)))
3 Likes

To write the string as a single flow of text and values, use f-string (which I much prefer):

print(f"{x} to the power of {y} is approximately {round(result)}.")

Another thing is that the function round() has really rare practical use. Because of multiple reasons it is unsuitable for rounding numbers for output. Instead of it use formatting:

print(f"{x} to the power of {y} is approximately {result:.0f}.")
2 Likes