본문 바로가기
python-기타

Python isnumeric()에 관한 건

by 무적김두칠 2022. 10. 21.

문자열중에 숫자를 찾고 싶을때 주로 썼는데 문득 궁금한게 생겨서 알아봤습니다

s="1a2b3c4d"
for i in range(len(s)):
    if s[i].isnumeric():
        print("%d index is numeric"%(i))

위와 같은 코드를 실행시키면 아래와 같은 결과가 나오게 됩니다.

1,2,3,4 가 numeric 하다는 뜻인데..  

s="1a2b3c4d"
answer=0
for i in range(len(s)):
    if s[i].isnumeric():
        print("%d index is numeric"%(i))
        answer+=s[i]

그럼 숫자에 대해 연산을 해볼까요? 

Type이 int형과 str형이라 다르다고 뜨네요

공식문서를 참고해봤습니다

신기한건 U+2155 ⅕ 도 numeric으로 쳐주네요
Vulgar fraction은 수학용어인데 분모가 10의 배수가 아닌 분수 라고보시면됩니다 

 

참조:

https://docs.python.org/3/library/stdtypes.html#str.isnumeric

 

Built-in Types — Python 3.10.8 documentation

Built-in Types The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some collection classes are mutable. The methods that

docs.python.org

 

반응형

댓글