[Tutor] Hello World in Python without space

Alan Gauld alan.gauld at btinternet.com
Sun Jul 10 15:40:34 CEST 2011


"Robert H" <hrobert25 at hotmail.com> wrote

> name="world"
> print("Hello", name,"!")
> Hello world !
>
> However, I don't want the space before the exclamation
> mark. I want this:
> Hello world!

> Can anyone out there help me? Thank you.

I see you've already had two answers, a third is
to construct the string before printing it. There
are various ways to do that:

The simplest:

output = "Hello " + name + "!"

An alternative which is more efficient for
larger numbers of substruings is:

output = "".join(["Hello ",name,"!"])  # thats an empty string to 
start

The thirs is to use a formatstring, but thats
what Izz did in his print call.

Whichever method you use you then use

print(output)

Lots of options. As Peter said, use the >>> prompt to
experiment to find which works best for you.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/







More information about the Tutor mailing list