[Tutor] Tutor Digest, Vol 89, Issue 22

ctak225 at gmail.com ctak225 at gmail.com
Mon Jul 11 15:32:43 CEST 2011


It just name+"!", this is string concatenation

tutor-request at python.org wrote:

>Send Tutor mailing list submissions to
>	tutor at python.org
>
>To subscribe or unsubscribe via the World Wide Web, visit
>	http://mail.python.org/mailman/listinfo/tutor
>or, via email, send a message with subject or body 'help' to
>	tutor-request at python.org
>
>You can reach the person managing the list at
>	tutor-owner at python.org
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of Tutor digest..."
>Today's Topics:
>
>   1. Hello World in Python without space (Robert H)
>   2. Re: Hello World in Python without space (Izz ad-Din Ruhulessin)
>   3. Re: Hello World in Python without space (Peter Otten)
>   4. Re: Hello World in Python without space (Alan Gauld)
>   5. Re: broken script - curiouser and curiouser (Lisi)
>
>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
>		 	   		  Sending args to the print command always puts spaces between
>them.
>
>Try:
>print("Hello {name}!".format(name=name))
>
>
>
>
>
>2011/7/10 Robert H <hrobert25 at hotmail.com>
>
>>  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 at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>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.
>
>
>
>"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/
>
>
>
>
>
>
>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 at python.org
>http://mail.python.org/mailman/listinfo/tutor

-- 
Sent from my Android phone with K-9 Mail. Please excuse my brevity.


More information about the Tutor mailing list