0

I have just started out with Python, and am using Idle.

I put in:

print("Professor Redwood: Right! So your name is",name,"!")

And got this in the console:

Professor Redwood: Right! So your name is Chase !

Notice the spaces around the name "Chase". I can't figure out how to get rid of these spaces. I looked up this question, and went to this page. I could not understand what the answer meant, so I need some help with it.

(Please note that I have already set the variable.)

2

3 Answers 3

4

By default, the Python print function uses a separation set to a single space when printing arguments; you can easily override this by defining sep='' (i.e empty space):

print("Professor Redwood: Right! So your name is",name,"!", sep='')

Which will now print:

Professor Redwood: Right! So your name isChase!

For Python 2.x you could either use from __future__ import print_function and if that doesn't suit you for some undisclosed reason, use join to explicitly join on the empty string:

print "".join(["Professor Redwood: Right! So your name is",name,"!"])

or, use .format as the other answer suggests:

print "Professor Redwood: Right! So your name is{0}!".format(name) 
Sign up to request clarification or add additional context in comments.

5 Comments

Good answer, of note, this only works with Python ≥ 3.0 by default.
Using join is slightly verbose.
@intboolstring agreed, just adding alternatives is all.
@Jim makes sense. I would definitely keep it in your answer.
another option for python 2 is to include the future import to make print a function
3

Another option using string concatenation:

print("Professor Redwood: Right! So your name is"+ name + "!")

You could also do the following using string formatting

print("Professor Redwood: Right! So your name is {0} !".format(name))

Comments

1

What you're trying to do is concatenate strings which isn't exactly what commas in the print statement do. Instead, concatenation can be achieved several ways, the most common of which is to use the + operator.

print("Professor Redwood: Right! So your name is " + name + "!")

or you can concatenate into a variable and print that. This is especially useful if you're concatenating lots of different strings or you want to reuse the string several times.

sentence = "Professor Redwood: Right! So your name is " + name + "!"
print sentence

or you can use string formatting. This has the benefit of usually looking cleaner and allowing special variable types such as decimals or currency to be formatted correctly.

print "Professor Redwood: Right! So your name is %s!" % name
print "Professor %s: Right! So your name is %s!" % (prof_name, name)
print "Professor Redwood: Right! So your name is {0}!".format(name)

Note that in Python 2, the print statement does not require brackets around the string. In Python 3 they are required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.