[Python] Summary of conversion between character strings and numerical values (ascii code)

Introduction

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.

Conversion method

You can convert each other with ʻord ('character')andchr (numerical value)`.

s = 'A'

ord_s = ord(s)
print(ord_s)  # 65

chr_s = chr(ord_s)
print(chr_s)  # A

Conversion table

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.

Important conversion pickup

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

Numbers

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

Uppercase alphabet

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

Lowercase alphabet

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

at the end

Thank you for browsing. Please point out any mistakes.

Recommended Posts

[Python] Summary of conversion between character strings and numerical values (ascii code)
Links and memos of Python character code strings
[Python] Various combinations of strings and values
Summary of the differences between PHP and Python
[Python] Conversion memo between time data and numerical data
3-3, Python strings and character codes
2.x, 3.x character code of python
[python] plot the values ​​before and after the conversion of yeojohnson conversion
Summary of Python indexes and slices
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
Summary of differences between Python and PHP (comparison table of main items)
[Python3] Switch between Shift_JIS, UTF-8 and ASCII
Conversion between singular and plural of words
[Python] Get the character code of the file
Correspondence summary of array operation of ruby and python
The answer of "1/2" is different between python2 and 3
python character code
Installation of Python3 and Flask [Environment construction summary]
I / O related summary of python and fortran
List of Python code to move and remember
Convert the character code of the file with Python3
[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)
[Python] Summary of eval / exec functions + How to write character strings with line breaks
[Python] Chapter 02-06 <Supplement> Basics of Python programs (handling of numerical values)
python2 series / 3 series, character code and print statement / command line
Python --Explanation and usage summary of the top 24 packages
[Python] Type Error: Summary of error causes and remedies for'None Type'
Difference between Ruby and Python in terms of variables
Indent behavior of json.dumps is different between python2 and python3
Mutual conversion between JSON and YAML / TOML in Python
[Python] Extract only numbers from lists and character strings
Summary of date processing in Python (datetime and dateutil)
[Python] Types of statistical values (features) and calculation methods
Numerical summary of data
About Python3 character code
Summary of Python arguments
Spit out a list of file name, last modified date and character code in python3
[python] Summary of how to retrieve lists and dictionary elements
[Python] Summary of how to use split and join functions
Summary of Hash (Dictionary) operation support for Ruby and Python
The process of making Python code object-oriented and improving it
A rough summary of the differences between Windows and Linux
Python: Create a dictionary from a list of keys and values
Mass generation of QR code with character display by Python
Summary of Differences Between Ruby on Rails and Django ~ Basics ~
Summary of python file operations
Summary of Python3 list operations
Conversion between unixtime and datetime
Adam Paper Summary and Code
Python --Check type of values
Character code learned in Python
Source installation and installation of Python
Basic map information using Python Geotiff conversion of numerical elevation data
Let's summarize the degree of coupling between modules with Python code
Story of speed comparison of sort of numerical value and character string (unfinished))
Search for character strings in files [Comparison between Bash and PowerShell]