Stefano Pesce
Stefano Pesce

Reputation: 1

Why does the print function leave a space at the end?

Why does it remain a space between Ste and !? How can I eliminate it, without using a difficult function?

a = input("Enter a name: ")

Enter a name: Ale

print("Hello,", a, "!")

Hello, Ste !

Upvotes: 0

Views: 181

Answers (3)

Bob
Bob

Reputation: 236

Comma , by default leaves a space after a string. You can try using +:

print("Hello, " + a + "!") =>Hello, Ste!

Upvotes: 1

Rajeshkumar
Rajeshkumar

Reputation: 59

Use the strip function to cut the space at the initial and end of the string.

print("Hello,", a.strip(), "!", sep="")

Upvotes: 0

Óscar López
Óscar López

Reputation: 235994

By default, print separates each of its arguments with a space. You can change it by specifying the sep parameter with something else, including an empty string. This should work:

print("Hello, ", a, "!", sep="")
=> Hello, Ale!

Upvotes: 1

Related Questions