프로그래밍 언어/Python
[python 문법] char 를 int로 변환 / int를 char로 변환
리그캣
2019. 1. 27. 21:49
알고리즘 문제를 풀다가
당황을 했다 기존 c , c++에서와는 다르게 파이썬에서 char를 숫자로 자동 변환이 안되었다.
예를 들면
print(int('a'))
위의 출력결과가 97? 아스키 코드로 나올줄 알았는데.. 에러가 떳다.
python에서는 ord라는 함수를 사용하여야 한다.
print(ord('a'))
97
Process finished with exit code 0
반대로 ord로 숫자로 변환햇을경우에는??
다음과 같이 chr로 다시 감싸주면 된다
print(chr(ord('a')))
a
Process finished with exit code 0
참고