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, because
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 working through
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')
eize...@gmail.com 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
Wayne Werner wrote:
On Tue, Mar 29, 2011 at 2:41 PM, Prasad, Ramit ramit.pra...@jpmchase.comwrote:
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
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:
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()
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
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, as far
On Tue, Mar 29, 2011 at 4:21 PM, Corey Richardson kb1...@aim.com 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 second
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
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 breamore...@yahoo.co.uk wrote:
On 29/03/2011 20:41, Prasad,
On Tue, Mar 29, 2011 at 2:41 PM, Prasad, Ramit ramit.pra...@jpmchase.comwrote:
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,
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:
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,
I
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
operator
Hi Kent,
Sorry I didn't make my question clearer. Bascially I
want to replace this line:
srm:socket portNumber=138 tcpORudp=UDP
address=64.41.134.60/
With:
srm:socket portNumber=2 tcpORudp=TCP
address=64.41.134.60/
So the regex grouping are that I want to keep
portNumber= and tcpORudp= and
Jerome Jabson wrote:
Hi Kent,
Sorry I didn't make my question clearer. Bascially I
want to replace this line:
srm:socket portNumber=138 tcpORudp=UDP
address=64.41.134.60/
With:
srm:socket portNumber=2 tcpORudp=TCP
address=64.41.134.60/
So the regex grouping are that I want to
On 7 Apr 2006, [EMAIL PROTECTED] wrote:
Sorry I didn't make my question clearer. Bascially I
want to replace this line:
srm:socket portNumber=138 tcpORudp=UDP
address=64.41.134.60/
With:
srm:socket portNumber=2 tcpORudp=TCP
address=64.41.134.60/
So the regex grouping are that I want
19 matches
Mail list logo