문자열 함수 중에 해당 문자열이 숫자로 구성됐는지 여부를 판별해주는 함수들이 있습니다.
isdecimal, isdigit, isnumeric 함수들인데요, 각각의 함수에 대해 알아보겠습니다.
isdecimal()
numbers = ['123', '0.5', '-7', '½', '3²', '77%']
for i in numbers:
print(i, i.isdecimal())
# 123 True
# 0.5 False
# -7 False
# ½ False
# 3² False
# 77% False
문자열을 구성하는 각 원소가 0과 9 사이의 정수(int)로 변환 가능한지 여부만을 판별합니다.
숫자로 보이더라도 정수화가 불가능하면 False를 리턴합니다.
isdigit()
numbers = ['123', '0.5', '-7', '½', '3²', '77%']
for i in numbers:
print(i, i.isdigit())
# 123 True
# 0.5 False
# -7 False
# ½ False
# 3² True
# 77% False
isdigit 메서드는 정수화가 가능한 문자열과 지수 형태의 문자열에 대해서 True를 리턴합니다.
isnumeric()
numbers = ['123', '0.5', '-7', '½', '3²', '77%']
for i in numbers:
print(i, i.isnumeric())
# 123 True
# 0.5 False
# -7 False
# ½ True
# 3² True
# 77% False
isnumeric 함수는 정수화가 가능한 문자열, 지수 형태의 문자열, 그리고 분수 형태의 문자열에 대해서 True를 리턴합니다.
'파이썬・ML' 카테고리의 다른 글
파이썬 객체에 대해서 (0) | 2023.06.05 |
---|---|
파이썬 itertools 모듈로 순열/조합/중복순열/중복조합 구하기 (0) | 2023.06.02 |
파이썬 datetime 모듈 사용해서 요일 알아내기 (0) | 2023.06.01 |
파이썬 enumerate 내장함수 사용하기 (0) | 2023.05.31 |
파이썬 map 내장함수 사용하기 (0) | 2023.05.31 |