a = '1234'
b = '1234 '
c = 1234
print(a)
print(b)
print(c)
위 파이썬 코드를 실행시켜보자.
1234
1234
1234
가 나올 것이다. str과 int가 구분이 안 되지 않는가? 그리고 같은 str끼리도 공백은 눈에 보이지 않으니 구분할 수가 없다.
그럼 어떻게 값을 정확하게 볼 수 있는가?
1. 리스트 안에 넣기
a = '1234'
b = '1234 '
c = 1234
print([a, b, c])
결과:
['1234', '1234 ', 1234]
타입과 공백을 명확하게 구분할 수 있다.
2. repr 함수 이용하기
a = '1234'
b = '1234 '
c = 1234
print(repr(a))
print(repr(b))
print(repr(c))
결과:
'1234'
'1234 '
1234
repr 함수가 무엇이냐 하면은...
>>> help(repr)
Help on built-in function repr in module builtins:
repr(obj, /)
Return the canonical string representation of the object.
For many object types, including most builtins, eval(repr(obj)) == obj.
help 함수를 통해 설명을 보면 저렇게 나온다. canonical string을 반환한다는 것 같다.
아무튼 그렇답니다
'python > uncategorized' 카테고리의 다른 글
python 표준 출력 표준 오류 파일로 저장 (0) | 2021.04.18 |
---|---|
dictionary changed size during iteration 해결 방법 (0) | 2021.04.18 |
컴퓨터 얼마나 켜졌는지 보기 (0) | 2021.04.18 |
py를 exe로 바꾸는 건 비효율적인 것 같다. (0) | 2021.04.18 |
python path 환경 변수 편집 (0) | 2021.04.18 |
댓글