Python Forum
Moving to the next character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving to the next character
#1
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()
Reply
#2
A is chr(65)

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

for a in range(65, 91):
    print(chr(a)
)
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] unexpected character after line continuation character paul18fr 4 3,410 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,182 Jul-13-2020, 07:05 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,742 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Replace changing string including uppercase character with lowercase character silfer 11 6,201 Mar-25-2019, 12:54 PM
Last Post: silfer
  SyntaxError: unexpected character after line continuation character Saka 2 18,572 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020