By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

string goes away

P: n/a
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
string.join(list_of_str, sep) instead of sep.join(list_of_str)
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: upper_list = map(string.upper, list_of_str)


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

Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hey Andreas,
I loved to use
>>> string.join(list_of_str, sep) instead of >>> sep.join(list_of_str)
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

Jul 18 '05 #2

P: n/a
Andreas Beyer wrote:
I loved to use
string.join(list_of_str, sep) instead of sep.join(list_of_str)


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)
Jul 18 '05 #3

P: n/a
Duncan Booth <du**********@invalid.invalid> writes:
[...]
str.join(sep, list_of_str)

[...]

Doesn't work with unicode, IIRC.

John
Jul 18 '05 #4

P: n/a
Andreas Beyer wrote:
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
Jul 18 '05 #5

P: n/a
"Martin v. Lwis" <ma****@v.loewis.de> writes:
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.
Jul 18 '05 #6

P: n/a
John J. Lee wrote:
Duncan Booth <du**********@invalid.invalid> writes:
[...]
str.join(sep, list_of_str)

[...]

Doesn't work with unicode, IIRC.

u" ".join(["What's", "the", "problem?"])

u"What's the problem?"

Jul 18 '05 #7

P: n/a
Dan Bishop wrote:
John J. Lee wrote:
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:
str.join(u" ", ["a", "b"]) 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.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 18 '05 #8

P: n/a
John J. Lee wrote:
Duncan Booth <du**********@invalid.invalid> writes:
[...]
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.
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.