In [37]:
    
#-*- coding: utf-8 -*-
    
문자의 구성
인코딩 (방식)
unicode 타입
encode(인코딩)/decode(디코딩) 명령 사용Python 3에서는 unicode 타입이 기본
__repr__()print() 명령
In [1]:
    
c = "a"
c
    
    Out[1]:
In [2]:
    
print(c), type(c)
    
    
In [3]:
    
#python2 기준. 인코딩 문제로 '가'가 아닌 특수문자들이 뜨는 경우가 있다. python3에서 실행할 경우에는 unicode로 되어 있기 때문에
#전부 정상적으로 한글 문자를 인식할 것이다.
x = "가"
x
    
    Out[3]:
In [4]:
    
print(x)
    
    
In [5]:
    
print(x.__repr__())
    
    
In [8]:
    
x = ["가"]
print(x), type(x)
    
    
In [9]:
    
x = "가"
len(x), type(x)
    
    Out[9]:
In [10]:
    
x = "ABC"
y = "가나다"
print(len(x), len(y))
print(x[0], x[1], x[2])
print(y[0], y[1], y[2])
print(y[0], y[1], y[2], y[3])
    
    
In [11]:
    
y = u"가"
y
    
    Out[11]:
In [12]:
    
print(y)
    
    
In [18]:
    
y = u"가나다"
print(y[0])
print(y[1])
print(y[2])
    
    
In [19]:
    
y = u"가나다"
print(y[0], y[1], y[2])
    
    
In [20]:
    
print(type(y))
z1 = y.encode("cp949")
print(type(z1))
print(z1)
    
    
In [21]:
    
print(type(y))
z2 = y.encode("utf-8")
print(type(z2))
print(z2)
    
    
In [22]:
    
print(type(z1))
y1 = z1.decode("cp949")
print(type(y1))
print(y1)
    
    
In [23]:
    
print(type(z1))
y2 = z2.decode("utf-8")
print(type(y2))
print(y2)
    
    
In [24]:
    
"가".encode("utf-8")
    
    
In [26]:
    
unicode("가", "ascii").encode("utf-8")
    
    
In [37]:
    
u"가".decode("utf-8")
    
    
In [27]:
    
u"가".encode("ascii").decode("utf-8")
    
    
디코드와 유니코드의 차이. 유니코드 포인트는 주민등록번호. 코드 포인트를 말함. 코드 포인트로 바꾸어 주는 것을 디코드라고 한다. 인코드는 그 반대
In [32]:
    
u"가".encode("utf-8"), u"가".encode("cp949"), "가"
    
    Out[32]:
In [33]:
    
u"가".encode("ascii")
    
    
In [34]:
    
import sys
print(sys.getdefaultencoding())
print(sys.stdin.encoding)
print(sys.stdout.encoding)
import locale
print(locale.getpreferredencoding())
    
    
PYTHONIOENCODING 로 지정가능#-*- coding: utf-8 -*-