Python2 string type

Python2 has two string types, str and ʻunicode. Normally you should use the ʻunicode type.

str type

str is more correct (I think) to be a byte string than a string.

aiueo = 'AIUEO'
#At this time, aiueo becomes str type

len(aiueo)
#How many will depend on the encoding of the file
#For example, utf-If it is 8, it becomes 15, and shift_If it is jis, it will be 10.

unicode type

The ʻunicode` type records characters as UCS-2 (or UCS-4). To use characters in the UCS-4 range, you need to specify it when compiling Python.

aiueo = u'AIUEO'
#At this time aiueo becomes unicode type

len(aiueo)
#Become 5 in any environment

encode and decode

You can convert it to ʻunicodetype by calling thedecode method of type str. Conversely, you can convert it to a str type by calling the ʻencode method of the ʻunicode` type.

aiueo = u'AIUEO'

aiueo_utf8 = aiueo.encode('utf-8')
aiueo_shiftjis = aiueo.encode('shift_jis')

print isinstance(aiueo_utf8, str) # True
print isinstance(aiueo_shiftjis, str) # True
print len(aiueo_utf8) # 15
print len(aiueo_shiftjis) # 10

print len(aiueo_utf8.decode('utf-8')) # 5
print len(aiueo_shiftjis.decode('shift_jis')) # 5

Passing an incorrect encoding to the decode method will result in a ʻUnicodeDecodeError` error.

aiueo_shiftjis.decode('utf-8') #UnicodeDecodeError error

Python3

It seems that str is changed to bytes and ʻunicode is changed to str` in Python3 because it is confusing.

reference

Recommended Posts

Python2 string type
Python # string type
Python: String concatenation
python string slice
Python string format
Python string inversion
Python basic course (4 numeric type / character string type)
Python callable type specification
String manipulation in python
[Python] Multi-line string assignment
Python string manipulation master
[Python2] Date string-> UnixTime-> Date string
Random string generation (Python)
Python3> documentation string / docstring
Check Python # type identity
Python string processing illustration
[python] Convert date to string
Python indentation and string format
Python
[Python] Use a string sequence
Python --Check type of values
Various Python built-in string operations
Python immutable type int memo
[Python 2/3] Parse the format string
String date manipulation in Python
Image data type conversion [Python]
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
Python f character (formatted string)
String format with Python% operator
[Python] Type Error:'in <string>' requires string as left operand, not list
Function argument type definition in python
[Translation] Python static type, amazing mypy!
Practice! !! Introduction to Python (Type Hints)
Python for super beginners Python # dictionary type 1 for super beginners
[python] week1-3: Number type and operation
Dynamically load json type in python
Type specified in python. Throw exceptions
6 ways to string objects in Python
[Personal memo] Python sequence type / mapping type
Master the type with Python [Python 3.9 compatible]
python string processing map and lambda
Create a random string in Python
Python for super beginners Python # dictionary type 2 for super beginners
Timezone specification when converting a string to datetime type in python
kafka python
Conversion of string <-> date (date, datetime) in Python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python technique
Studying python
Shell type
Python 2.7 Countdown
Python memorandum
Reintroduction to Python Decorators ~ Learn Decorators by Type ~
[Python] Easy argument type check with dataclass
python tips
python function ①
Python basics
Python memo