Why is there an extra space in between a variable and punctuation in a string? Python

Question:

There is an extra space in the output of a string. This is the section of the code where it happens. It happens in the string of the function, nameConfirmation().

def chooseName():
    name = ""
    name = raw_input("Let's begin with your name. What is it? ")

    return name

def nameConfirmation():
    name = chooseName()
    print ("Right... So your name is", name,".")

This is the output it gives.

Right... So your name is Raven .

How do I get rid of the space in between the name and the punctuation?

Asked By: Destiny

||

Answers:

The comma indicates a continuation on the same line with a space in between (you’ll notice there’s a space between “is” and “Raven”, even though neither string has one in there). If you want to remove the spaces, the usual way to concatenate strings is with a plus

Edit: Plus, not ampersand… stupid me

Answered By: neophlegm

You can append string with +.

print ("Right... So your name is", name + ".")

Output:

Right... So your name is Raven.
Answered By: Mervyn Lee

Each argument passed through the print command is automatically separated by a space. I see you’re in python 2, and I use python 3, so I’m not sure if this solution will work, but you can try doing this:

print ("Right... So your name is ", name,".",sep='')

This basically (assuming it works in python 2 as well) changes the separation between arguments to no space instead of 1 space.

Answered By: Jacob Rodal

Use the string variable substitution with %s as in:

def nameConfirmation():
    name = chooseName()
    print ("Right... So your name is %s." % name)

or use the .format() function that is a built-in method for str in:

def nameConfirmation():
    name = chooseName()
    output = "Right... So your name is {}.".format(name)
    print(output)

or you could use the shorter f"" format as in:

def nameConfirmation():
    name = chooseName()
    print(f"Right... So your name is {name}.")

For more on string manipulation and formatting in Python3, Check the following:

https://www.digitalocean.com/community/tutorials/an-introduction-to-string-functions-in-python-3
https://www.digitalocean.com/community/tutorials/an-introduction-to-working-with-strings-in-python-3
https://www.digitalocean.com/community/tutorials/how-to-use-string-formatters-in-python-3

Answered By: Ouss

If you use:

print ("Right... So your name is", name,".")

You will notice that the output is:

Right... So your name is Raven .

Look at is and name (Raven). You can note in the output an space (is Raven), that is because print() have a default argument called sep an by default it’s print("Right... So your name is", name,".", sep = ' '). That argument is an string that it’s added at the end of each piece of string concatenated with a coma , in the print function.
So if you do print('A','B') it will be A B, because when you concatenate A and B, print will add ' ' (and space) as glue.
You can configure it: print('A','B', sep='glue') would print AglueB!

To solve your problem you can do two options.

  • Change sep = '' and add an space after is: print ("Right... So your name is ", name,".", sep='')
  • Or, concatenate using + the last two strings: print ("Right... So your name is", name + ".")

Also there are a lot of another ways like: (I ordered them by worst to best in my subjetive opinion…)

  • print("Right... So your name is %s." % name).
  • print("Right... So your name is {}.".format(name)).
  • print(f"Right... So your name is {name}.")

Link with documentation:

P.S: This isn’t part of the answer, it’s just a note.

  • print (something) don’t need the space –> print(something).
  • Futhemorer sep = ' ' there is also called end = 'n' that determine the end of a print (n = new line).

P.S 2: Thanks Ouss for the idea of add some documentations links. I’ve just learnt that you can do print(%(key)s % mydict)!

Answered By: Ender Look

The reason why does the code produces a extra space is because the default sep looks like this:

sep = ' '

And the seperator seperates the values/input because by default sep contains a space. So that’s why even though you did not put a blank space it still does the job for you.

Just in case you don’t know what is sep, it is a short form for separator.

There are 3 easy solutions to solve this problem:

  • b escape sequence.

  • '+' string concatanation

  • sep=''


1st Solution (b escape sequence):

print ("Right... So your name is", name,"b.")

2nd Solution ('+' string concatanation):

print ("Right... So your name is", name + ".")

3rd Solution (sep=''):

print ("Right... So your name is ", name,".",sep='')

All of them produces same output:

Right... So your name is Raven.
Answered By: CPP_is_no_STANDARD
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.