User:Manetta/Prototyping-SI21: Difference between revisions
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 13: | Line 13: | ||
Can we print the Unicode table? | Can we print the Unicode table? | ||
==DIWO encodings== | |||
<syntaxhighlight lang=python> | |||
import sys | |||
# https://devdocs.io/python~3.9/library/sys#sys.stdin | |||
# https://devdocs.io/python~3.9/library/exceptions#KeyError | |||
d = { | |||
'a':'lalala', | |||
'b':'hahaha', | |||
'c':'blablabla' | |||
} | |||
try: | |||
for line in sys.stdin: | |||
words = line.split() | |||
for word in words: | |||
for character in word.strip(): | |||
print(d[character], end='') | |||
print() | |||
except KeyError as error: | |||
print(f"oops! i can't handle this character: { error }") | |||
</syntaxhighlight> |
Latest revision as of 09:39, 25 April 2023
Unicode in Python
One-character Unicode strings can also be created with the chr() built-in function, which takes integers and returns a Unicode string of length 1 that contains the corresponding code point. The reverse operation is the built-in ord() function that takes a one-character Unicode string and returns the code point value. https://docs.python.org/3/howto/unicode.html
for i in range(128):
chr(i)
Can we print the Unicode table?
DIWO encodings
import sys
# https://devdocs.io/python~3.9/library/sys#sys.stdin
# https://devdocs.io/python~3.9/library/exceptions#KeyError
d = {
'a':'lalala',
'b':'hahaha',
'c':'blablabla'
}
try:
for line in sys.stdin:
words = line.split()
for word in words:
for character in word.strip():
print(d[character], end='')
print()
except KeyError as error:
print(f"oops! i can't handle this character: { error }")