Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
^C is 3
#1
if you have "^C", what Python code can give the value 3 for it? ord('\n') gives 10, but ord('^C') does not give 3. this is because len("^C") is 2 and ord() only works with strings that have a len() of 1.
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
Yes, try list(map(ord, '^C'))
Then you'll get 2 numbers.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I would say:

def myord(c):
    if len(c) == 2 and c[0] == '^':
        return ord(c[1]) - ord('@')
    else:
        return ord(c)

print(myord('^C'))
Links: caret notation, ascii control characters.
Reply
#4
but there is nothing built into Python to do this. i think i will implement superord() that does everything (but not the laundry).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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