Connecting Tech Pros Worldwide Forums | Help | Site Map

string goes away

Andreas Beyer
Guest
 
Posts: n/a
#1: Jul 19 '05
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[color=blue][color=green][color=darkred]
>>> string.join(list_of_str, sep)[/color][/color][/color]
instead of[color=blue][color=green][color=darkred]
>>> sep.join(list_of_str)[/color][/color][/color]

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:[color=blue][color=green][color=darkred]
>>> upper_list = map(string.upper, list_of_str)[/color][/color][/color]

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


alex23
Guest
 
Posts: n/a
#2: Jul 19 '05

re: string goes away


Hey Andreas,
[color=blue]
> I loved to use[color=green][color=darkred]
> >>> string.join(list_of_str, sep)[/color][/color]
> instead of[color=green][color=darkred]
> >>> sep.join(list_of_str)[/color][/color]
>
> I think the former is much more telling what is happening than the
> latter. However, I will get used to it.[/color]

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

Hope this helps.

-alex23

Duncan Booth
Guest
 
Posts: n/a
#3: Jul 19 '05

re: string goes away


Andreas Beyer wrote:
[color=blue]
> I loved to use[color=green][color=darkred]
> >>> string.join(list_of_str, sep)[/color][/color]
> instead of[color=green][color=darkred]
> >>> sep.join(list_of_str)[/color][/color]
>
> I think the former is much more telling what is happening than the
> latter. However, I will get used to it.[/color]

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)
John J. Lee
Guest
 
Posts: n/a
#4: Jul 19 '05

re: string goes away


Duncan Booth <duncan.booth@invalid.invalid> writes:
[...][color=blue]
> str.join(sep, list_of_str)[/color]
[...]

Doesn't work with unicode, IIRC.



John
Martin v. Löwis
Guest
 
Posts: n/a
#5: Jul 19 '05

re: string goes away


Andreas Beyer wrote:[color=blue]
> 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.[/color]

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
Paul Rubin
Guest
 
Posts: n/a
#6: Jul 19 '05

re: string goes away


"Martin v. Löwis" <martin@v.loewis.de> writes:[color=blue]
> 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?[/color]

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.
Dan Bishop
Guest
 
Posts: n/a
#7: Jul 19 '05

re: string goes away


John J. Lee wrote:[color=blue]
> Duncan Booth <duncan.booth@invalid.invalid> writes:
> [...][color=green]
> > str.join(sep, list_of_str)[/color]
> [...]
>
> Doesn't work with unicode, IIRC.[/color]
[color=blue][color=green][color=darkred]
>>> u" ".join(["What's", "the", "problem?"])[/color][/color][/color]
u"What's the problem?"

Greg Ewing
Guest
 
Posts: n/a
#8: Jul 19 '05

re: string goes away


Dan Bishop wrote:[color=blue]
> John J. Lee wrote:
>[color=green]
>>Doesn't work with unicode, IIRC.[/color]
>[color=green][color=darkred]
>>>>u" ".join(["What's", "the", "problem?"])[/color][/color]
>
> u"What's the problem?"[/color]

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:
[color=blue][color=green][color=darkred]
>>> str.join(u" ", ["a", "b"])[/color][/color][/color]
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:
[color=blue][color=green][color=darkred]
>>> str.join(" ", [u"a", u"b"])[/color][/color][/color]
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
Duncan Booth
Guest
 
Posts: n/a
#9: Jul 19 '05

re: string goes away


John J. Lee wrote:
[color=blue]
> Duncan Booth <duncan.booth@invalid.invalid> writes:
> [...][color=green]
>> str.join(sep, list_of_str)[/color]
> [...]
>
> Doesn't work with unicode, IIRC.
>
>[/color]
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.
Closed Thread