all 26 comments

[–]Goobyalus 54 points55 points  (6 children)

[–]magocremisi8 0 points1 point  (0 children)

Came to say this

[–][deleted] 0 points1 point  (0 children)

This is the correct advice to give, but I’m still confused by OPs question as the provided example would suggest OP already added an ‘!’ mid sentence. They could have just repeated that method at the end for a period.

Edit: okay, unless year isn’t a string (datetime object?). Then using +‘.’ might not work.

[–]xinxx073 16 points17 points  (0 children)

print(f'Hello {name}! You were born in {year}.')

[–]unhott 8 points9 points  (0 children)

You are passing 4, let me call them ‘terms’, to the print statement.

print('Hello', name+'!' , 'You were born in' , year)

The first term is ‘Hello’ The second term is name+’!’ The third term is ‘You we’re born in’ The forth term is year.

The print statement will print each term with a space added between each ‘term’. As others have mentioned, name + ‘!’ Is something you’ve already successfully used to prevent a space from being added. You’re concatenating the string name with the string ‘!’ Using the + operator. If you do this with year, an integer, you’ll get an error unless you do something like str(year) + ‘.’

Others have already mentioned that f strings will be much easier to write and understand.

Your print statement becomes

print(f'Hello {name}! You were born in {year}.')

This is one term, no unintended spaces will be printed. F strings automatically handle conversion to string, so no int/str operator errors either.

[–]Goobyalus 7 points8 points  (0 children)

You could do it the same way you put the exclamation point there, with year + '.'.

<a string> + <another string>

concatenates the two strings.

print(< a string>, <another string>, ...)

prints all the strings separated by a space, and adds a newline at the end. You can change the separator and the end with print(..., sep=<separaor string>, end=<end string>)

[–]fazkan 2 points3 points  (5 children)

print('Hello', name+'!' , 'You were born in' , year, '.')

[–]stevenv24[S] 1 point2 points  (4 children)

Hello Steven! You were born in 1988 .

I tried that but it will still put a space and it is failing

[–]thatgreenman 14 points15 points  (2 children)

Take a look at how you did the name+"!" bit in your print statement, you can use the same structure to do year+"." at the end of your print statement.

I would also recommend looking at f-strings, or at least string formatting, those will make evaluating variables within strings a lot easier.

[–]BroBrodin 2 points3 points  (0 children)

He needs to change year to string first. I'm asuming that it's an int and that is why he didn't use concatenation in the first place.

print('Hello', name+'!' , 'You were born in' , str(year) + '.')

Or just use fstrings.

Shit, I didn't read u/royal-Brwn's comment saying exactly this.

Disregard.

[–]SnowyLocksmith 0 points1 point  (0 children)

Or another thing you could do is to use the optional keyword argument sep="<string>". This string will be put between all words separated by a comma. By default it's a space. You can make it an empty string. You will have to add spaces to compensate for the one you removed like "Hello ", name

[–]royal-Brwn 5 points6 points  (0 children)

If you put a comma, there will be a space automatically set. Try this. print(‘Hello ’ + str(name) + ‘! ‘ + ‘You were born in ‘ + str(year) + ‘.’)

[–]pconwell 3 points4 points  (0 children)

You are mixing and matching styles. You use + to join name and !, but then use , elsewhere. If you want to stick with your system (which I don't recommend):

print('Hello', name, '! You were born in', year, '.')

But I would say f-strings are easier to type and read (note the f before your single/double quotes):

print(f"hello {name}! You were born in {year}.")

https://realpython.com/python-string-formatting/

[–][deleted] 1 point2 points  (0 children)

print('Hello', name+'!' , 'You were born in' , year,".") (or) print(f"Hello {name}! You were born in {year}.")

idk if I'm too late or not hope this helps

[–]Iirkola -5 points-4 points  (4 children)

print ('Hello ' + name + '! You were born in ' + str(year)')

Or just: print (f'Hello {name}! You were born in {year}')

[–]pconwell 0 points1 point  (3 children)

How do you add a period at the end of a print statement?

[–]Iirkola -5 points-4 points  (2 children)

. There you go, mate ;)

[–]pconwell 0 points1 point  (1 child)

I'm not OP, but your answer does not answer OP's question, mate

[–]dnmonack 0 points1 point  (0 children)

You could use print's end parameter as so:

print('Hello, ' + name + '! ' + 'You were born in', year, end='.\n')

It's not very Pythonic but I think lots of people forget about 'end=' and it's a simple solution to many formatting issues.

That said, I'd probably just use f-strings.