manipulate string

C

Chris

I heard that strings are immutable, but isn't there regardless a way to
manipulate a string?
I have a string that looks like this:
a = '0123456789'
But I want it to look like this:
a = '0 - 2 - 4 - 6 - 8 - '
I want whitespace between every number and I want to fade out every
second number.
It is funny that I can explicitly read parts of that string, print a[3]
works perfectly, but I can't remove it, delete it or what ever.

I would appreciate any hint on how to solve my little "problem".

Thanks a lot.
Regards, Tom
 
M

Michel Claveau/Hamster

Like this :


import string

a='0123456789'
l=list(a)
b=string.join(l,' - ')
print b



@-salutations
 
M

Michel Claveau/Hamster

2 by 2 :


import string
a='0123456789'
l=list(a[::2])
b=string.join(l,' - ')
print b
 
C

Chris

Thanks guys.
I knew it couldn't be too difficult, but I just didn't know in which
direction I should search for a solution. :)

Thanks again.
Regards, Tom
 
C

Chris

It is me again.
Unfortunately it doesn't work. Python doesn't accept [::2]
TypeError: sequence index must be integer
I use Python Release 2.2.3.
Any ideas?

-Tom
 
W

Werner Schiendl

Hi,

Eric said:
Apparently, the OP wants to discard characters at indexes 1, 3, 5, 7,
etc... So, assuming Python version is 2.3, the correct solution seems to
be:
a='0123456789'
print ' - '.join(a[::2])
0 - 2 - 4 - 6 - 8

almost :)

the original poster said he wanted

a = '0 - 2 - 4 - 6 - 8 - '

which means the last faded-out 9 is missing in your solution


the following works:
>>> a = '0123456789'
>>>
>>> b = " ".join( [i % 2 and "-" or a for i in range(len(a))] )
>>> b

'0 - 2 - 4 - 6 - 8 -'


I think the trailing space is a typo of the OP...


hth

Werner
 
D

David C. Fox

import string
a='0123456789'
l=list(a[::2])
b=string.join(l,' - ')
print b
It is me again.
Unfortunately it doesn't work. Python doesn't accept [::2]
TypeError: sequence index must be integer
I use Python Release 2.2.3.
Any ideas?

-Tom

Yeah, the x[::i] notation for strings and lists only works in 2.3.

A couple of alternatives to l = list(a[::2]) which work in 2.2 are:

l = []
for i in range( (len(a) + 1) // 2):
l.append(a[2*i])

or more compactly

l=[a[2*i] for i in range((len(a)+1)//2)]

or

l = [a for i in range(len(a)) if 2*(i//2) == i]

David
 
D

David C. Fox

David said:
import string
a='0123456789'
l=list(a[::2])
b=string.join(l,' - ')
print b

It is me again.
Unfortunately it doesn't work. Python doesn't accept [::2]
TypeError: sequence index must be integer
I use Python Release 2.2.3.
Any ideas?

-Tom

Yeah, the x[::i] notation for strings and lists only works in 2.3.

A couple of alternatives to l = list(a[::2]) which work in 2.2 are:

l = []
for i in range( (len(a) + 1) // 2):
l.append(a[2*i])

or more compactly

l=[a[2*i] for i in range((len(a)+1)//2)]

or

l = [a for i in range(len(a)) if 2*(i//2) == i]

David


or

l = [a for i in range(0, len(a), 2)]

David
 
C

Chris

Thanks for all the help.
I just decided to update to Python 2.3 anyway.

Thanks to all, Tom
 
S

Skip Montanaro

>>> s = "0123456789"
>>> " - ".join([s for i in range(0, 10, 2)])

'0 - 2 - 4 - 6 - 8'

I thought the original poster wanted the odd numbers replaced by '-' and then
all elements separated by spaces. None of the ' - '.join(...) examples
achieve that, as they fail to replace '9' with '-'. Here's an alternative
which does:
>>> s = "0123456789"
>>> [int(c)%2 and '-' or c for c in s] ['0', '-', '2', '-', '4', '-', '6', '-', '8', '-']
>>> ' '.join([int(c)%2 and '-' or c for c in s])
'0 - 2 - 4 - 6 - 8 -'

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top