6

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?

Destiny
  • 65
  • 1
  • 5

6 Answers6

7

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)!

Ender Look
  • 2,195
  • 2
  • 18
  • 36
  • I like your answer. Very informative and complete. Please consider adding some links to references/tutorials on strings manipulation and formatting in python3 at the end of the answer! – Ouss Dec 07 '17 at 03:00
  • @Ouss, ok, I will start searching! – Ender Look Dec 07 '17 at 03:01
4

You can append string with +.

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

Output:

Right... So your name is Raven.
Mervyn Lee
  • 1,687
  • 3
  • 24
  • 51
1

Use the string variable substitution %s:

def nameConfirmation():
    name = chooseName()
    print ("Right... So your name is %s." % 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

Ouss
  • 1,905
  • 1
  • 20
  • 34
0

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

neophlegm
  • 365
  • 1
  • 13
0

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.

Jacob Rodal
  • 560
  • 1
  • 4
  • 11
0
class account:
    def __init__(self,name,acctype,balance):
        self.name = name
        self.acctype = acctype;
        #let us create private instance variable
        self.__balance = balance
    def show(self):
        print("name =",self.name)
        print("account type=",self.acctype,sep='')
        print("balance = ",self.__balance, "/-",sep='')
        #use sep='' to remove space from left and right side of variable
        
A1 = account("ankit patel","current",1000)
print("before change of amount")
A1.show()
A1.balance = 123456789
A1.__balance = 123456789
print("after change of amount")
A1.show()