[python] isdecimal(), isdigit(), isnumeric()

JunYoung7·2022년 5월 12일
0

isdecimal(), isdigit(), isnumeric() 모두 문자열이 숫자로 이루어졌는지 검사하는 함수이다.

a = '1234'
b = '3²'
c = '1/2'

print(a.isdigit()) # true
print(a.isdecimal()) # true
print(a.isnumeric()) # true

print(b.isdigit()) # true
print(b.isdecimal()) # false
print(b.isnumeric()) # true

print(c.isdigit()) # false
print(c.isdecimal()) # false
print(c.isnumeric()) # true
  • isdigit() => 해당 문자열이 숫자로만 이루어져 있으면 True
  • isdecimal() => 해당 문자열이 0~9의 수, 즉 int형으로 바로 변환될 수 있으면 True
  • isnumeric() => 좀 더 광범위하게 인정. 즉 '수로 볼 수 있는 형태'이면 True (거듭제곱, 분수형 등등)
profile
JunYoung7의 프로그래밍 발자취

0개의 댓글

관련 채용 정보