0

I'm doing a project for school and I'm having some issues trying to get this white space out of it. Keep in mind I'm very much a beginner at all of this. It goes as follows:

print('What is your name?')
user_name = input()
print('How old are you?')
user_age = int(input())
birth_year = (2023 - user_age)
print('Hello',user_name,"!",'You were born in',birth_year,".")

Everything but the last line works perfectly, because once it runs everything and goes to print this, I get something that looks like this:

Hello e ! You were born in 2011 .

Is there any way I could fix the code to get rid of this whitespace? It's driving me crazy. Thanks in advance.

I did try to change things around by getting rid of commas and adding + signs and tampering with it anyway I could and I only got syntax errors.

Ro.oT
  • 623
  • 6
  • 15
Emmy_T
  • 3
  • 2
  • Maybe this will make things clear: https://stackoverflow.com/questions/19457227/how-to-print-like-printf-in-python3 – kzi Jul 07 '23 at 14:36
  • Construct the single string you want to print yourself, instead of letting `print` assemble the string from multiple arguments. – chepner Jul 07 '23 at 21:23

2 Answers2

0

Replace this:

print('Hello',user_name,"!",'You were born in',birth_year,".")

with the following:

print('Hello %s! You were born in %d.' % (user_name, birth_year))
Ro.oT
  • 623
  • 6
  • 15
  • Ohhhhh my gosh thank you. I've been suck on this a hot minute, I appreciate this so much! – Emmy_T Jul 07 '23 at 14:43
  • No worries, if you find this answer helpful, please "Accept" it! :)) – Ro.oT Jul 07 '23 at 14:46
  • 2
    `f'Hello {user_name}! You were born in {birth year}.'` would be more commonly recommended nowadays. The `%` operator is quite old and has relatively few advantages over f-strings. – chepner Jul 07 '23 at 21:24
-1

Change the separator with sep="" inside the print list.

https://www.geeksforgeeks.org/python-sep-parameter-print/

Yves Daoust
  • 672
  • 9
  • 1
    That would eliminate whitespace *after* the `!` as well (never mind the whitespace between "Hello" and the name, for example). – chepner Jul 07 '23 at 21:24
  • @chepner: what a remark ! The difficulty was to avoid the unwanted space. Adding one is immediate ! Thanks for the downvote :( – Yves Daoust Jul 08 '23 at 10:06
  • And yet, you haven't bothered to actually fix the answer yet. – chepner Jul 09 '23 at 16:22
  • @chepner: stop this harassment. I gave the essential ingredient and I have no reason to publish updated code. – Yves Daoust Jul 09 '23 at 16:39