Concatenating values in a print statement
Solution 1
The reason why it's printing unexpectedly is because in Python 2.x, print is a statement, not function, and the parentheses and space are creating a tuple. Print can take parentheses like in Python 3.x, but the problem is the space between the statement and parentheses. The space makes Python interpret it as a tuple. Try the following:
print "Hello ", name
Notice the missing parentheses. print
isn't a function that's called, it's a statement. It prints Hello Bob
because the first string is and the variable separated by the comma is appended or concatenated to the string that is then printed. There are many other ways to do this in Python 2.x.
Solution 2
There are several ways to archive this:
Python 2:
>>> name = "Bob"
>>> print("Hello", name)
Hello Bob
>>> print("Hello %s" % name)
Hello Bob
Python 3:
>>> name = "Bob"
>>> print("Hello {0}".format(name))
Hello Bob
Both:
>>> name = "Bob"
>>> print("Hello " + name)
Hello Bob
Solution 3
In Python 2.x, you can print out directly
print "Hello", name
But you can also format your string
print ("Hello %s" % name)
print "Hello {0}".format(name)
Solution 4
print("Hello" + ' ' + name)
should do it on python 2
Solution 5
Replace your comma (,
) with a plus sign (+
), and add some appropriate spacing.
Code
name = "Bob"
print("Hello " + name)
Output
Hello Bob
Auden Young
They/them, please! I'm an undergraduate student at UC Berkeley and a moderator on Quantum Computing SE. Research I'm currently doing research with the Quantum Devices Group at Berkeley. Some personal projects include reading about spontaneous parametric down conversion and working on an experiment related to that - I've finished building a TEA laser, and am working on obtaining the crystals and other optics I need. I also finished the ideal half of a quantum computer simulator I'm working on - please feel free to try it out (it's on my GitHub)! I'm working on adding stuff that will simulate noise and decoherence in the qubits, and a few other features I'm planning. I also recently fell down a delightful rabbit hole on shark intestines - turns out that the type of spiral intestine sharks have does not correlate to diet, so I'm teaching myself R and parts of phylogenetics to try to test different hypotheses for what the different types of spirals correlate with. Reading I always enjoy learning new things about Python and have been playing around with machine learning for a bit. I'm also starting to learn more about Lisp since parts of it were covered in a class I took in fall semester. I also enjoy DMing for a D&D game I run. Currently reading through a collection of Marlowe's plays - Tamburlaine is wild.
Updated on July 09, 2022Comments
-
Auden Young almost 2 years
I have the string "Bob" stored in a variable called
name
.I want to print this:
Hello Bob
How do I get that?
Let's say I have the code
name = "Bob" print ("Hello", name)
This gives
('Hello', 'Bob')
Which I don't want. If I put in the code
name = "Bob" print "Hello" print name
This gives
Hello Bob
Which is also not what I want. I just want plain old
Hello Bob
How do I get that?
I apologize in advance if this is a duplicate or dumb question.
-
Bart over 7 yearsI thought that
"{}".format()
was the standard in Python3? -
linusg over 7 yearsFormatting using the percent character is deprecated in Python 3. It was replaced by the
str.format()
method. -
rafaelc over 7 years@Bart I was just editing
-
Auden Young over 7 yearsThank you, this answer helps explain the solution better.
-
Auden Young over 7 yearsThank you, this answer explains why the solution works.
-
Andrew Li over 7 yearsAlso note that the space between the print statement in and the parentheses effected the outcome. Without the space it should work fine
-
Auden Young over 7 yearsFWIW, I was planning on accepting the answer; I was just waiting for the 9 minutes to be up. =) Also, good to know. Thanks again!