I’m new to Python, coming from Java and C. How can I increment a char? In Java or C, chars and ints are practically interchangeable, and in certain loops, it’s very useful to me to be able to do increment chars, and index arrays by chars.
How can I do this in Python? It’s bad enough not having a traditional for(;;) looper – is there any way I can achieve what I want to achieve without having to rethink my entire strategy?
In Python 2.x, just use the
ordandchrfunctions:Python 3.x makes this more organized and interesting, due to its clear distinction between bytes and unicode. By default, a “string” is unicode, so the above works (
ordreceives Unicode chars andchrproduces them).But if you’re interested in bytes (such as for processing some binary data stream), things are even simpler: