Python Forum
Moving to the next character - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Moving to the next character (/thread-19585.html)



Moving to the next character - 357mag - Jul-05-2019

I'm experimenting with a C++ style program trying to make it work in Python but I don't think it's gonna go. I'm trying to increment what is in the letter variable to get it to print the letter that follows after 'A' in the alphabet. This works okay in C++ but not working in Python. What do I have to do to make it work in Python?

letter = 'A'

print("The current character is " + letter)
letter += 1
print("The current character is" + letter)

print()



RE: Moving to the next character - Axel_Erfurt - Jul-05-2019

A is chr(65)

x = 65
print(chr(x))
x += 1
print(chr(x))
or

for a in range(65, 91):
    print(chr(a)
)


RE: Moving to the next character - snippsat - Jul-05-2019

if want to see letter input.
>>> from_letter = 'a'
>>> to_letter = 'z'
>>> [chr(c) for c in range(ord(from_letter), ord(to_letter))]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y']
In Python 3 a lot changed regarding Unicode.
>>> [chr(c) for c in range(9812, 9818)]
['♔', '♕', '♖', '♗', '♘', '♙']

# Other way around 
>>> [chr(c) for c in range(ord('♔'), ord('♙'))]
['♔', '♕', '♖', '♗', '♘']
>>> from unicodedata import name

>>> chess = [chr(c) for c in range(9812, 9818)]
>>> for c in chess:
...     if name(c, '-') != '-':
...         print(f'{c} | {ord(c):04x} | {name(c)}')

♔ | 2654 | WHITE CHESS KING
♕ | 2655 | WHITE CHESS QUEEN
♖ | 2656 | WHITE CHESS ROOK
♗ | 2657 | WHITE CHESS BISHOP
♘ | 2658 | WHITE CHESS KNIGHT
♙ | 2659 | WHITE CHESS PAWN