본문 바로가기

Developer/Python

Python 문자열 활용 : isdigit isalpha isalnum

1. isdigit()

문자열이 모두 숫자로 구성 : True

그 외 : False

str1 = '12345'
str2 = '12 34 5'
str3 = '12 34 5 ab'

print(str1.isdigit())  # True
print(str2.isdigit())  # False
print(str3.isdigit())  # False

 

2. isalpha()

문자열이 모두 문자로 구성 : True

그 외 : False

str1 = 'hello'
str2 = 'hello12'
str3 = 'hello hello'

print(str1.isalpha())  # True
print(str2.isalpha())  # False
print(str3.isalpha())  # False

 

3. isalnum()

문자열이 문자 또는 숫자로 구성 : True

그 외 : False

str1 = 'hello'
str2 = 'hello12'
str3 = 'hello hello'

print(str1.isalnum())  # True
print(str2.isalnum())  # True
print(str3.isalnum())  # False