Python Forum
^C is 3 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ^C is 3 (/thread-11995.html)



^C is 3 - Skaperen - Aug-04-2018

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.


RE: ^C is 3 - DeaD_EyE - Aug-04-2018

Yes, try list(map(ord, '^C'))
Then you'll get 2 numbers.


RE: ^C is 3 - Gribouillis - Aug-04-2018

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.


RE: ^C is 3 - Skaperen - Aug-05-2018

but there is nothing built into Python to do this. i think i will implement superord() that does everything (but not the laundry).