Re: [Tutor] String formatting question with 's'.format()

2011-06-08 Thread Alan Gauld
wrote 'first={0}, last={1}, middle={2}'.format(*parts) "first=S, last=M, middle=['P', 'A']" why do we need the '*' at 'parts'. I know we need it, because otherwise it gives an error: The * tells Python to unpack parts and treat the contents as individual values. format is looking for 3 v

Re: [Tutor] String formatting question with 's'.format()

2011-06-08 Thread Evgeny Izetov
I see now, that example helps. Basically I use one asterisk to extract a list or a tuple and double asterisks for a dictionary, but I have to provide keys in case of a dictionary, like here: >>> template = '{motto}, {pork} and {food}' >>> a = dict(motto='spam', pork='ham', food='eggs') >>> templat

Re: [Tutor] String formatting question with 's'.format()

2011-06-08 Thread Prasad, Ramit
From: tutor-bounces+ramit.prasad=jpmchase@python.org [mailto:tutor-bounces+ramit.prasad=jpmchase@python.org] On Behalf Of eize...@gmail.com Sent: Wednesday, June 08, 2011 3:11 PM To: tutor@python.org Subject: [Tutor] String formatting question with 's'.format() I'm work

[Tutor] String formatting question with 's'.format()

2011-06-08 Thread eizetov
I'm working through the 'Learn Python' book by Mark Lutz, in this example: somelist = list('SPAM') parts = somelist[0], somelist[-1], somelist[1:3] 'first={0}, last={1}, middle={2}'.format(*parts) "first=S, last=M, middle=['P', 'A']" why do we need the '*' at 'parts'. I know we need it, becaus

Re: [Tutor] String formatting question.

2011-03-31 Thread Steve Willoughby
On 31-Mar-11 09:46, bob gailer wrote: IMHO % formatting is the easiest to use and understand. I am sorry that it has been slated for removal. I had the same reaction, but I think it was mostly because of my long background as a C programmer, since it's essentially the equivalent of printf() f

Re: [Tutor] String formatting question.

2011-03-31 Thread bob gailer
IMHO % formatting is the easiest to use and understand. I am sorry that it has been slated for removal. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python

Re: [Tutor] String formatting question.

2011-03-31 Thread Steven D'Aprano
Wayne Werner wrote: On Tue, Mar 29, 2011 at 2:41 PM, Prasad, Ramit wrote: Is there a difference (or preference) between using the following? "%s %d" % (var,num) VERSUS "{0} {1}".format(var,num) Practically there's no difference. In reality (and under the hood) there are more differences, som

Re: [Tutor] String formatting question.

2011-03-29 Thread Wayne Werner
On Tue, Mar 29, 2011 at 2:41 PM, Prasad, Ramit wrote: > Is there a difference (or preference) between using the following? > "%s %d" % (var,num) > VERSUS > "{0} {1}".format(var,num) > Practically there's no difference. In reality (and under the hood) there are more differences, some of which are

Re: [Tutor] String formatting question.

2011-03-29 Thread Modulok
For simple strings I use the "%s" % foo version, for more complex stuff I use the .format() method. I find it easier to control spacing and alignments with the .format() method, but that's just me. -Modulok- On 3/29/11, Blockheads Oi Oi wrote: > On 29/03/2011 20:41, Prasad, Ramit wrote: >> Is t

Re: [Tutor] String formatting question.

2011-03-29 Thread Blockheads Oi Oi
On 29/03/2011 20:41, Prasad, Ramit wrote: Is there a difference (or preference) between using the following? "%s %d" % (var,num) VERSUS "{0} {1}".format(var,num) Ramit ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription opti

Re: [Tutor] String formatting question.

2011-03-29 Thread James Reynolds
On Tue, Mar 29, 2011 at 4:21 PM, Corey Richardson wrote: > On 03/29/2011 03:41 PM, Prasad, Ramit wrote: > > Is there a difference (or preference) between using the following? > > "%s %d" % (var,num) > > VERSUS > > "{0} {1}".format(var,num) > > > > > > Ramit > > If you're using Python 3, use the s

Re: [Tutor] String formatting question.

2011-03-29 Thread Corey Richardson
On 03/29/2011 03:41 PM, Prasad, Ramit wrote: > Is there a difference (or preference) between using the following? > "%s %d" % (var,num) > VERSUS > "{0} {1}".format(var,num) > > > Ramit If you're using Python 3, use the second one. If you're using Python 2, you have no option but to use the first

[Tutor] String formatting question.

2011-03-29 Thread Prasad, Ramit
Is there a difference (or preference) between using the following? "%s %d" % (var,num) VERSUS "{0} {1}".format(var,num) Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This communication is for infor

Re: [Tutor] string formatting question

2006-04-07 Thread Karl Pflästerer
On 7 Apr 2006, [EMAIL PROTECTED] wrote: > Sorry I didn't make my question clearer. Bascially I > want to replace this line: > > address="64.41.134.60"/> > > With: > > address="64.41.134.60"/> > > So the regex grouping are that I want to keep > portNumber= and tcpORudp= and replace the values. >

Re: [Tutor] string formatting question

2006-04-07 Thread Kent Johnson
Jerome Jabson wrote: > Hi Kent, > > Sorry I didn't make my question clearer. Bascially I > want to replace this line: > > address="64.41.134.60"/> > > With: > > address="64.41.134.60"/> > > So the regex grouping are that I want to keep > portNumber= and tcpORudp= and replace the values. > Wh

Re: [Tutor] string formatting question

2006-04-07 Thread Jerome Jabson
Hi Kent, Sorry I didn't make my question clearer. Bascially I want to replace this line: With: So the regex grouping are that I want to keep portNumber= and tcpORudp= and replace the values. Which will be varibles in my code. The question is more on the string formatting in the replace. Ho

Re: [Tutor] string formatting question

2006-04-07 Thread Alan Gauld
> My problem now is how do I construct the replace > statement? > twork = m_sock.sub('\1 %s \2 %s', % port_num % proto, > twork) The format operator takes a tuple: twork = m_sock.sub('\1 %s \2 %s' % (port_num, proto), twork) So I removed the comma after the string, used a single percent operat

Re: [Tutor] string formatting question

2006-04-07 Thread Kent Johnson
Jerome Jabson wrote: > Hello, > > I'm trying to replace some strings in a line of text, > using some regex functions. My question is: If there's > more then one regex grouping I want to replace in one > line of a file, how can I use the String Formatting > operator (%s) in two places? Hi Jerome,

[Tutor] string formatting question

2006-04-07 Thread Jerome Jabson
Hello, I'm trying to replace some strings in a line of text, using some regex functions. My question is: If there's more then one regex grouping I want to replace in one line of a file, how can I use the String Formatting operator (%s) in two places? Here's the line it matches in the file: Her