string goes away

A

alex23

Hey Andreas,
I loved to use

I think the former is much more telling what is happening than the
latter. However, I will get used to it.

I find that binding a name to the separator makes it more readable
(YMMV):
>>> list_of_str = ['a','b','c']
>>> comma = ','
>>> comma.join(list_of_str)
'a,b,c'
But what about this:
upper_list = map(string.upper, list_of_str)
>>> upper_list = [s.upper() for s in list_of_str]
>>> upper_list
['A', 'B', 'C']

Hope this helps.

-alex23
 
A

Andreas Beyer

Hi:

If I am getting the docs etc. correctly, the string-module is depricated
and is supposed to be removed with the release of Python 3.0.
I still use the module a lot and there are situations in which I don't
know what to do without it. Maybe you can give me some help.

I loved to use
I think the former is much more telling what is happening than the
latter. However, I will get used to it.

But what about this:
What am I supposed to do instead?

I am sure there has been lots of discussion on whether or not to remove
the string module. Maybe you can just direct me to the right place.

Thanks for help!
Andreas
 
D

Duncan Booth

Andreas said:
I loved to use

I think the former is much more telling what is happening than the
latter. However, I will get used to it.

No need to get used to it. Just reverse the order of the arguments and use:

str.join(sep, list_of_str)

Alternatively it can be clearer if you bind a name to the bound method:

joinLines = '\n'.join
joinWords = ' '.join

lines = joinLines(somelines)
words = joinWords(somewords)
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Andreas said:
If I am getting the docs etc. correctly, the string-module is depricated
and is supposed to be removed with the release of Python 3.0.
I still use the module a lot and there are situations in which I don't
know what to do without it. Maybe you can give me some help.

Out of curiosity: when thinking about Python 3.0, what is the timespan
in which you expect that to appear? Before 2010? After 2010? After 2020?

Regards,
Martin
 
P

Paul Rubin

Martin v. Löwis said:
Out of curiosity: when thinking about Python 3.0, what is the timespan
in which you expect that to appear? Before 2010? After 2010? After 2020?

I'm not terribly worried about Python 3.0 incompatibilities, whenever
those are. There are already three incompatible Python versions
(CPython, Jython, IronPython) with PyPy coming right along. If any of
those newer ones take off in popularity, there's going to be much more
interoperability hassle than 3.0 will cause.
 
G

Greg Ewing

Dan said:
John said:
Doesn't work with unicode, IIRC.
u" ".join(["What's", "the", "problem?"])

u"What's the problem?"

str.join(x, y) isn't quite a drop-in replacement for
string.join(y, x), since it's not polymorphic on the
joining string:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: descriptor 'join' requires a 'str' object but received a 'unicode'

The strings being joined can be unicode, though:
>>> str.join(" ", [u"a", u"b"])
u'a b'

So it's probably not a serious problem, since in most
cases you'll know whether the joining string is unicode
or not when you write the code. If not, you'll just
have to do it the "new" way.
 
D

Duncan Booth

John said:
Duncan Booth said:
str.join(sep, list_of_str)
[...]

Doesn't work with unicode, IIRC.
str.join won't work if sep is unicode, but generally you know what type the
separator is and str.join will quite happily join a list of strings where
one or more is unicode and return a unicode result.

If you know the separator is unicode then use unicode.join instead.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top