Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
the next higher character
#1
i have a character as a string of length 1. i want the next higher character. 'a'->'b' or '4'->'5' or '9'->':'. i could do chr(ord(ch)+1). does python have any more elegant way?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
#!/usr/bin/python3
import string

for ch in string.ascii_letters:
    print(ch)
Reply
#3
what character does that code yield?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
See:
https://docs.python.org/3.7/library/string.html
Reply
#5
You can use a bytearray of length 1, which is a writable array containing a single integer in the range [0, 256). You can use the latin-1 encoding to convert such an array to / from a str instance of length 1 (this is the same as the utf8 encoding for ords below 128).
>>> t = bytearray('a', encoding='latin-1')
>>> t
bytearray(b'a')
>>> t[0] += 5
>>> t
bytearray(b'f')
>>> t.decode('latin-1')
'f'
Reply
#6
Great.
Reply
#7
(Jun-07-2019, 02:33 AM)heiner55 Wrote: See:
https://docs.python.org/3.7/library/string.html

i think you didn't understand my question. i do not see an answer in that link.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Because your question are always very complex.
Reply
#9
if the question is too hard to understand why post any answer at all?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
I did not think my answer was so bad.

#!/usr/bin/python3
import string
 
for ch in string.ascii_letters:
    print(ch)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner Higher Lower Game wallytan 2 1,539 Sep-29-2022, 05:14 PM
Last Post: deanhystad
  Regex higher IR library DreamingInsanity 6 2,134 Jun-06-2022, 02:37 PM
Last Post: DreamingInsanity
  [solved] unexpected character after line continuation character paul18fr 4 3,294 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,107 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,674 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  finding the next higher representable floating point value Skaperen 0 1,916 Sep-13-2019, 11:16 PM
Last Post: Skaperen
  Replace changing string including uppercase character with lowercase character silfer 11 6,072 Mar-25-2019, 12:54 PM
Last Post: silfer
  SyntaxError: unexpected character after line continuation character Saka 2 18,475 Sep-26-2017, 09:34 AM
Last Post: Saka
  How do I loop through a list and delete numerical elements that are 1 lower/higher? neko 4 4,256 Sep-05-2017, 02:25 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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