1
import datetime
from datetime import date
today = date.today()
user_name = input('What is your name? ')
user_age = int(input('How old are you? '))

print('Hello ' + user_name + '! You were born in', today.year - user_age, '.')

What is your name? Tyler How old are you? 31 Hello Tyler! You were born in 1991 .

Process finished with exit code 0

how do i get rid of the space between the period at the end?

I tried using + but it wont work because its an integer

  • 2
    Don't give `print()` separate arguments with `,`, but construct a single string with `+` (like you did in the first half), or better yet use an f-string (like `f'Hello {user_name}!'` etc.). – Grismar Nov 05 '22 at 02:20
  • `print(f"Hello {user_name}! You were born in {today.year - user_age}.")` – vignesh kanakavalli Nov 05 '22 at 02:21
  • Does this answer your question? [Remove specific characters from a string in Python](https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python) – damonholden Nov 05 '22 at 02:26
  • Thank you so much. Im currently in school and this is only my 2nd week learning how to code so I've got tons to learn. Thanks for the help. – Tyler Dunkle Nov 05 '22 at 02:27

4 Answers4

2

Use an f string, just like this!

import datetime
from datetime import date
today = date.today()
user_name = input('What is your name? ')
user_age = int(input('How old are you? '))
print(f"Hello {user_name}! You were born in {today.year - user_age}.")
iiSpidey
  • 21
  • 3
0

You can convert the year into a string by wrapping it in the str() function and then use + to concatenate.

0

print() adds spaces and new lines by default. You can utilize the sep argument to prevent spaces being injected:

print('Hello ' + user_name + '! You were born in ', today.year - user_age, '.', sep='')

See here:

How do I keep Python print from adding newlines or spaces?

Virt
  • 13
  • 4
0

Use an f-string to print for greater control. By default print uses a space as a separator between seperate items.

print(f"Hello {user_name}! You were born in {today.year - user_age}.")
Chris
  • 26,361
  • 5
  • 21
  • 42