[CentralOH] chr()/ord() Ugliness

michael at yanovich.net michael at yanovich.net
Mon Nov 11 16:34:00 CET 2013


I think it might be string.uppercase

string.uppercase returns a string of all the capital letters in ASCII.

In [1]: import string

In [2]: string.uppercase
Out[2]: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

and consequently there is a string.lowercase too, but you can shove
and index into it and get a specific letter back:

In [4]: string.uppercase[3]
Out[4]: 'D'

On 11/11/2013 10:29 AM, jep200404 at columbus.rr.com wrote:
> 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]. 
> 
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh
> 
> 

-- 
Michael Yanovich

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 901 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/centraloh/attachments/20131111/52c43229/attachment-0001.sig>


More information about the CentralOH mailing list