Re: [Tutor] string replacement in Python 2 and 3

2013-11-27 Thread Albert-Jan Roskam
On Wed, 11/27/13, Steven D'Aprano wrote: Subject: Re: [Tutor] string replacement in Python 2 and 3 To: tutor@python.org Date: Wednesday, November 27, 2013, 12:36 AM On Tue, Nov 26, 2013 at 11:42:29AM -0800, Albert-Jan Roskam wrote:

Re: [Tutor] string replacement in Python 2 and 3

2013-11-26 Thread Steven D'Aprano
On Tue, Nov 26, 2013 at 11:42:29AM -0800, Albert-Jan Roskam wrote: > Hi, > > String replacement works quite differently with bytes objects in > Python 3 than with string objects in Python 2. What is the best way to > make example #1 below run in Python 2 and 3? If you are working with text str

[Tutor] string replacement in Python 2 and 3

2013-11-26 Thread Albert-Jan Roskam
Hi, String replacement works quite differently with bytes objects in Python 3 than with string objects in Python 2. What is the best way to make example #1 below run in Python 2 and 3? Should one really decode the bytes keys and (if applicable) the values to unicode? The code gets so bloated wi

Re: [Tutor] String Replacement question

2008-05-22 Thread Faheem
Hey All, Thanks.. That was quick. Good to know there is a mailing list like this one. Thanks to everyone and all the replies. Faheem ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] String Replacement question

2008-05-22 Thread wesley chun
> > some = 'thing' > > print '%s %s %s %s' % (some,some,some,some) > > You can use named parameters, which moves the repetition to the format string: > > In [24]: print '%(some)s %(some)s %(some)s %(some)s' % (dict(some=some)) > thing thing thing thing > > With multiple values, a common trick

Re: [Tutor] String Replacement question

2008-05-22 Thread Ricardo Aráoz
Faheem wrote: Hi all, How do I replace the same value multiple times without repeating the same variable name/value repeatedly? for ex. some = 'thing' print '%s %s %s %s' % (some,some,some,some) in this case, my question is how do i replace "% (some,some,some)" with something more concis

Re: [Tutor] String Replacement question

2008-05-21 Thread bob gailer
Faheem wrote: Hi all, How do I replace the same value multiple times without repeating the same variable name/value repeatedly? for ex. some = 'thing' print '%s %s %s %s' % (some,some,some,some) in this case, my question is how do i replace "% (some,some,some)" with something more concis

Re: [Tutor] String Replacement Question

2008-05-21 Thread Carlos Hanson
On Wed, May 21, 2008 at 9:39 AM, Jeff Younker <[EMAIL PROTECTED]> wrote: > On May 21, 2008, at 6:42 AM, Kinuthia Muchane wrote: > > st = "String" > print "%s " %st*3 >> >> String String String > >> >> Does this help? > > Another approach is to use dictionaries for string > replacement.

Re: [Tutor] String Replacement Question

2008-05-21 Thread Jeff Younker
On May 21, 2008, at 6:42 AM, Kinuthia Muchane wrote: st = "String" print "%s " %st*3 String String String Does this help? Another approach is to use dictionaries for string replacement. >>> subst = {'some': 'thing'} >>> print "%(some)s%(some)s%(some)s" % subst thingthingthing -jeff ___

Re: [Tutor] String Replacement Question

2008-05-21 Thread Kinuthia Muchane
Moishy Gluck wrote: >>> "%s " %st*3 'String String String ' ^ If you look closely at the end of the string there is an extra space. >>> " ".join((st, )*3) 'String String String' ^ No extra space. Ah...Okay! On Wed, May 21, 2008 at

Re: [Tutor] String Replacement Question

2008-05-21 Thread Moishy Gluck
uot; %st*3 >>>>> >>>> String String String >> >>> >>>>> >> Does this help? >> >> Kinuthia... >> >> - >> >> Message: 6 >> Date: Wed, 21 May 2008 15:05:21 +0530 >

[Tutor] String Replacement Question

2008-05-21 Thread Kinuthia Muchane
st = "String" print "%s " %st*3 String String String Does this help? Kinuthia... - Message: 6 Date: Wed, 21 May 2008 15:05:21 +0530 From: Faheem <[EMAIL PROTECTED]> Subject: [Tutor] String Replacement question To: tutor@python.org Mes

Re: [Tutor] String Replacement question

2008-05-21 Thread Moishy Gluck
Some other solutions might be >>> animal = 'cat' >>> " ".join((animal, ) * 4) 'cat cat cat cat' >>> animal = 'cat' >>> print " ".join((animal, ) * 4) cat cat cat cat >>>#If you need the string injected into another string >>> print "My %s's name is ginger." % (" ".join((animal,) * 4)) My cat cat c

Re: [Tutor] String Replacement question

2008-05-21 Thread Kent Johnson
On Wed, May 21, 2008 at 5:35 AM, Faheem <[EMAIL PROTECTED]> wrote: > Hi all, > How do I replace the same value multiple times without repeating the > same variable name/value repeatedly? > for ex. > > some = 'thing' > print '%s %s %s %s' % (some,some,some,some) You can use named parameters, whi

Re: [Tutor] String Replacement question

2008-05-21 Thread Wolfram Kraus
Am 21.05.2008 11:35, Faheem schrieb: Hi all, How do I replace the same value multiple times without repeating the same variable name/value repeatedly? for ex. some = 'thing' print '%s %s %s %s' % (some,some,some,some) in this case, my question is how do i replace "% (some,some,some)" wit

[Tutor] String Replacement question

2008-05-21 Thread Faheem
Hi all, How do I replace the same value multiple times without repeating the same variable name/value repeatedly? for ex. some = 'thing' print '%s %s %s %s' % (some,some,some,some) in this case, my question is how do i replace "% (some,some,some)" with something more concise? thanks in adv

Re: [Tutor] string replacement

2007-05-21 Thread Luke Paireepinart
Chandrashekar wrote: > Hi, > > I am trying to do something like this in python. Can you please help? > > handle= open('test.txt','a') > > handle.write(''' > > Testsomething$i > > something$i-test > > ''' > ) > > when i write into the file, i would like to have the output like this. > > Testsomethin

Re: [Tutor] string replacement

2007-05-21 Thread Tom Tucker
One way is... CODE #!/usr/bin/python handle = file("test.txt",'a') number = 1 for num in range(1,5): handle.write("TestingSome%s\n" % (number)) handle.write("something%s-test\n" % (number)) number += 1 handle.close() OUTPUT TestingSome1 something1-

Re: [Tutor] string replacement

2007-05-21 Thread Andreas Kostyrka
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Well, there are a number of ways to achieve this, but the classical one would be: """abc%(i)s def%(i)s """ % dict(i=1) Andreas Chandrashekar wrote: > Hi, > > I am trying to do something like this in python. Can you please help? > > handle= open('t

[Tutor] string replacement

2007-05-21 Thread Chandrashekar
Hi, I am trying to do something like this in python. Can you please help? handle= open('test.txt','a') handle.write(''' Testsomething$i something$i-test ''' ) when i write into the file, i would like to have the output like this. Testsomething1 something1-test Testsomething2 something2-test