I have summarized how to calculate characters and ascii code in Python for myself. It has been confirmed to work with Python 3.4.3 on AtCoder.
You can convert each other with ʻord ('character')and
chr (numerical value)`.
s = 'A'
ord_s = ord(s)
print(ord_s) # 65
chr_s = chr(ord_s)
print(chr_s) # A
As a summary, the correspondence table between ascii code and characters is described. The item ʻascii code` in the table below shows the notation in decimal.
ascii code | Hexadecimal | letter |
---|---|---|
48 | 0x30 | 0 |
57 | 0x39 | 9 |
... | ... | ... |
65 | 0x41 | A |
90 | 0x5a | Z |
... | ... | ... |
97 | 0x61 | a |
122 | 0x7a | z |
For example, uppercase and lowercase letters can be converted by converting them to ascii code and then + 32
. (Of course you can also use .replace ()
)
s = 'A'
small_s = chr(ord(s)+32)
print(small_s) # a
Please note that the numbers here are ** numbers as letters **.
ascii code | Hexadecimal | letter |
---|---|---|
48 | 0x30 | 0 |
49 | 0x31 | 1 |
50 | 0x32 | 2 |
51 | 0x33 | 3 |
52 | 0x34 | 4 |
53 | 0x35 | 5 |
54 | 0x36 | 6 |
55 | 0x37 | 7 |
56 | 0x38 | 8 |
57 | 0x39 | 9 |
ascii code | Hexadecimal | letter |
---|---|---|
65 | 0x41 | A |
66 | 0x42 | B |
67 | 0x43 | C |
68 | 0x44 | D |
69 | 0x45 | E |
70 | 0x46 | F |
71 | 0x47 | G |
72 | 0x48 | H |
73 | 0x49 | I |
74 | 0x4a | J |
75 | 0x4b | K |
76 | 0x4c | L |
77 | 0x4d | M |
78 | 0x4e | N |
79 | 0x4f | O |
80 | 0x50 | P |
81 | 0x51 | Q |
82 | 0x52 | R |
83 | 0x53 | S |
84 | 0x54 | T |
85 | 0x55 | U |
86 | 0x56 | V |
87 | 0x57 | W |
88 | 0x58 | X |
89 | 0x59 | Y |
90 | 0x5a | Z |
ascii code | Hexadecimal | letter |
---|---|---|
97 | 0x61 | a |
98 | 0x62 | b |
99 | 0x63 | c |
100 | 0x64 | d |
101 | 0x65 | e |
102 | 0x66 | f |
103 | 0x67 | g |
104 | 0x68 | h |
105 | 0x69 | i |
106 | 0x6a | j |
107 | 0x6b | k |
108 | 0x6c | l |
109 | 0x6d | m |
110 | 0x6e | n |
111 | 0x6f | o |
112 | 0x70 | p |
113 | 0x71 | q |
114 | 0x72 | r |
115 | 0x73 | s |
116 | 0x74 | t |
117 | 0x75 | u |
118 | 0x76 | v |
119 | 0x77 | w |
120 | 0x78 | x |
121 | 0x79 | y |
122 | 0x7a | z |
Thank you for browsing. Please point out any mistakes.
Recommended Posts