S
SL
How can I compute with the integer values of characters in python?
Like
'a' + 1 equals 'b' etc
Like
'a' + 1 equals 'b' etc
SL said:How can I compute with the integer values of characters in python?
Like
'a' + 1 equals 'b' etc
2008/4/30 Gary Herron said:You can get an integer value from a character with the ord() function.
How can I compute with the integer values of characters in python?
Like 'a' + 1 equals 'b' etc
Lutz Horn said:Hi,
So just for completion, the solution is:
'b'
thanksI'm a beginner and I was expecting this to be a member of
string so I couldnt find it anywhere in the docs.
Gabriel Genellina said:And that's a very reasonable place to search; I think chr and ord are
builtin functions (and not str methods) just by an historical
accident. (Or is there any other reason? what's wrong with "a".ord()
or str.from_ordinal(65))?
Arnaud Delobelle said:Not a reason, but doesn't ord() word with unicode as well?
Gabriel Genellina said:En Wed, 30 Apr 2008 04:19:22 -0300, SL <[email protected]> escribió:
And that's a very reasonable place to search; I think chr and ord are
builtin functions (and not str methods) just by an historical accident.
(Or is there any other reason? what's wrong with "a".ord() or
str.from_ordinal(65))?
Not a reason, but doesn't ord() word with unicode as well?
Torsten said:However, join() is really bizarre. The list rather than the
separator should be the leading actor.
However, join() is really bizarre. The list rather than the
separator should be the leading actor.
separator should be the leading actor.
Hallöchen!
*Maybe* for aesthetical reasons. I find ord(c) more pleasent for
the eye. YMMV.
The biggest ugliness though is ",".join(). No idea why this should
be better than join(list, separator=" ").
It is only ugly because you aren't used to seeing method calls on string
literals.
George said:def join(iterable, sep=' ', encode=str):
return sep.join(encode(x) for x in iterable)
Actually
return encode(sep).join(encode(x) for x in iterable)
lest you get TypeErrors for non-string separators.
Seconded. While we're at it, a third optional 'encode=str' argument
should be added, to the effect of:def join(iterable, sep=' ', encode=str):
return sep.join(encode(x) for x in iterable)I can't count the times I've been bitten by TypeErrors raised on
','.join(s) if s contains non-string objects; having to do
','.join(map(str,s)) or ','.join(str(x) for x in s) gets old fast.
"Explicit is better than implicit" unless there is an obvious default.
I'm afraid I don't agree with you on this. Most places where I use join
I already know the type of the values being joined. In those few cases
where I want to join non strings, or want to do some other processing on
the values the generator comprehension is easy and has the advantage
that I can use a more complex expression than a simple function call
without having to wrap it in a lambda or otherwise contort things:
e.g. comma.join(x[1] for x in s)
vs. comma.join(s, encode=operator.itemgetter(1))
or comma.join(s, encode=lambda x: x[1])
Plus of course, you aren't going to be writing ','.join(str(x) for x in
s) more than once in any given program before you extract it out to a
function, are you?
Duncan said:It is only ugly because you aren't used to seeing method calls on string
literals.
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.