Python/Python Basic
Python Basic : isnumeric (숫자로 변환될 수 있는 data인지 체크하기)
CosmosProject
2021. 3. 18. 20:00
728x90
반응형
Python에 있는 isnumeric() method는 String type의 데이터에 적용할 수 있으며,
해당 String data가 숫자로 전환될 수 있는 data라면 True,
숫자로 전환될 수 없는 data라면 False를 반환합니다.
text1 = '1'
print(text1.isnumeric())
text2 = '1a'
print(text2.isnumeric())
text2 = 'Text Text'
print(text2.isnumeric())
-- Result
True
False
False
위 예시를 봅시다.
text1 -> 비록 따옴표로 1이라는 문자가 String으로서 되어있지만, 1은 숫자로 전환될 수 있겠죠? 따라서 isnumeric은 True를 반환합니다.
text2 -> 1a는 String이며 a라는 문자 때문에 당연히 숫자로 변환될 수 없습니다. 따라서 isnumeric은 False를 반환합니다.
text3 -> Text Text라는 문자도 당연히 숫자로 변환될 수 없으니 isnumeric은 False를 반환합니다.
728x90
반응형