Python Forum

Full Version: ^C is 3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
Yes, try list(map(ord, '^C'))
Then you'll get 2 numbers.
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.
but there is nothing built into Python to do this. i think i will implement superord() that does everything (but not the laundry).