0

I want to "translate" integer values in the range from 0 to 26 to characters 'A' to 'Z', but that seems harder in python than in C++, where it is allowed to add integer values to char variables.

The other attempt to generate an array with consecutive characters from a range is apparently also not possible;
I know that I could create an "ABC...XYZ" string for that purpose, but I don't want to do that.

Question.
how can character-ranges à la

ABC = [c for c in range('A','Z')]+['Z'] 

be generated, resp. emulated in python?

2
  • I really dislike closing a question as a duplicate, when you can only know it's a duplicate once you already know the answer. Commented May 18, 2024 at 15:23
  • @MarkRansom The answer may be the same, but the question is different, therefore I leave it up to the philosophers whether we have a duplicate or not... Commented May 18, 2024 at 15:39

1 Answer 1

1

Python has a pair of functions ord and chr for converting between a character and an integer. So you can use the regular range function with integers.

ABC = [chr(c) for c in range(ord('A'), ord('Z')+1)] 
Sign up to request clarification or add additional context in comments.

1 Comment

Although in this case and maybe most real cases it might be better to import it from string...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.