Compare strings in Python

Why you need it

When I was studying for the Python engineer certification exam, I understood what I could not understand by comparing character strings by comparison operation, so I will summarize it.

What it is

 >>> 'ABC'<'C'<'Pascal'<'Python'
True

Why is the above comparison True? → Because the character code is compared

In the first place, the character code point is preset by python. You can easily check it with ʻord (string) . For example, ʻord ('a') is 97, ʻord ('A') is 65, and so on. The above string comparison compares these numbers. However, even when comparing the code points of strings, ʻord ('ABC') returns a type error.

actually

 >>> 'ABC'<'C'<'Pascal'<'Python'
True

Note that the string comparison compares the points of the first character code. ʻOrd ('A') = 65 <ʻord ('C') = 67. Since'Pascal'and'Python' start with'P', we will continue to compare the letters'a'and'y'. Since ʻord ('a') = 97 <ʻord ('y') = 121,'Pascal'<'Python'is correct.

Recommended Posts

Compare strings in Python
Reverse strings in Python
Search for strings in Python
Extract strings from files in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Bulk replacement of strings in Python arrays
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python