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:
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
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
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
> > 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
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
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
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.
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
___
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
uot; %st*3
>>>>>
>>>> String String String
>>
>>>
>>>>>
>> Does this help?
>>
>> Kinuthia...
>>
>> -
>>
>> Message: 6
>> Date: Wed, 21 May 2008 15:05:21 +0530
>
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
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
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
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
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
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
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-
-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
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
20 matches
Mail list logo