[Tutor] Hello World in Python without space
Dear all, I have Python 3.2 installed on Windows 7. I am a complete beginner playing around with the basic functions. My problem is the following script: name="world" print("Hello", name,"!") The result is: Hello world ! However, I don't want the space before the exclamation mark. I want this: Hello world! I tried to solve the problem with e.g.: print("Hello",name.strip(),"!") but the result is the same. Can anyone out there help me? Thank you. Regards, Robert ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
Sending args to the print command always puts spaces between them. Try: print("Hello {name}!".format(name=name)) 2011/7/10 Robert H > Dear all, > > > I have Python 3.2 installed on Windows 7. I am a complete beginner playing > around with the basic functions. My problem is the following script: > > > name="world" > print("Hello", name,"!") > > > The result is: > Hello world ! > > > However, I don't want the space before the exclamation mark. I want this: > Hello world! > > > I tried to solve the problem with e.g.: > print("Hello",name.strip(),"!") > but the result is the same. > > > Can anyone out there help me? Thank you. > > > Regards, > Robert > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
Robert H wrote: > I have Python 3.2 installed on Windows 7. I am a complete beginner playing > around with the basic functions. My problem is the following script: > > > name="world" > print("Hello", name,"!") > > > The result is: > Hello world ! > > > However, I don't want the space before the exclamation mark. I want this: > Hello world! > > > I tried to solve the problem with e.g.: > print("Hello",name.strip(),"!") > but the result is the same. print() by default inserts a space between its arguments. You can avoid that by specifying a separator explicitly with the "sep" keyword. Let me show it in the interactive interpreter which is generally a good place to experiment with small snippets of code: >>> name = "Robert" >>> print("Hello ", name, "!", sep="") # Note the explicit " " after "Hello" Hello Robert! Another goodie is that you can easily get useful information about modules, classes, keywords, and functions, e. g. >>> help(print) shows print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. Use help() without argument to learn more about the interactive help. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
"Robert H" 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/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] broken script - curiouser and curiouser
On Thursday 07 July 2011 15:36:06 Michael M Mason wrote: > Maybe you've been editing ex25 and then executing ex26. That'd get you the > same error in the same place over and over again. Doh! They do say that there is one born every minute. Thanks, Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor