[CentralOH] chr()/ord() Ugliness

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Mon Nov 11 16:29:41 CET 2013


On Mon, 11 Nov 2013 10:02:41 -0500, William McVey <wam at cisco.com> wrote:

> On 11/11/2013 09:51 AM, jep200404 at columbus.rr.com wrote:
> > What is a better way of accomplishing the following?
> >      chr(ord('A') + i)

> You've listed a method of accomplishing something, 
> but you didn't really specify specifically what you're looking 
> to do. 

I want to get a letter that is i greater than 'A', 
while iterating over arbitrary iterable.
Imagine something like:

    for i, foo in enumerate(bar):
        print 'Entity %s is %s' % (chr(ord('A') + i), foo)

Since my original posting, I've thought of the following, 
but I don't know if it's better. Separating the ugliness 
of ord() and chr() might just make it harder to understand.

    a = ('hello', 'world', 'foo', 'bar')
    for c, foo in enumerate(a, ord('A')):
        print 'Entity %s is %s' % (chr(c), foo)

> Assuming you want to 
> iterate over each of the uppercase letters ...

I'm iterating over some arbitrary iterator 
and need to generate letter 'indexes' for printing.

> ... or pull letters out by offset, ... 

I don't have a list of letters from which to pull. 

>     import string
>     string.upper[i]

I understand 'hello world'.upper(), but not string.upper[i]. 



More information about the CentralOH mailing list