티스토리 뷰

728x90

String Validators

Python has built-in string validation methods for basic data. It can check if a string is composed of alphabetical characters, alphanumeric characters, digits, etc.

str.isalnum()
This method checks if all the characters of a string are alphanumeric (a-z, A-Z and 0-9).

>>> print 'ab123'.isalnum()
True
>>> print 'ab123#'.isalnum()
False

str.isalpha()
This method checks if all the characters of a string are alphabetical (a-z and A-Z).

>>> print 'abcD'.isalpha()
True
>>> print 'abcd1'.isalpha()
False

str.isdigit()
This method checks if all the characters of a string are digits (0-9).

>>> print '1234'.isdigit()
True
>>> print '123edsd'.isdigit()
False

str.islower()
This method checks if all the characters of a string are lowercase characters (a-z).

>>> print 'abcd123#'.islower()
True
>>> print 'Abcd123#'.islower()
False

str.isupper()
This method checks if all the characters of a string are uppercase characters (A-Z).

>>> print 'ABCD123#'.isupper()
True
>>> print 'Abcd123#'.isupper()
False

Task

You are given a string S.
Your task is to find out if the string S contains: alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters.

Input Format

A single line containing a string S.

Constraints

Output Format

In the first line, print True if S has any alphanumeric characters. Otherwise, print False.
In the second line, print True if S has any alphabetical characters. Otherwise, print False.
In the third line, print True if S has any digits. Otherwise, print False.
In the fourth line, print True if S has any lowercase characters. Otherwise, print False.
In the fifth line, print True if S has any uppercase characters. Otherwise, print False.

Sample Input

qA2

Sample Output

True
True
True
True
True

 

문제해석

파이썬에는 아래와 같이 string validator 함수들이 있다.

1. isalnum() - 문자열의 모든 값이 알파벳 또는 숫자인 경우 true를 리턴한다. (a-z, A-Z, 0-9)

2. isalpha() - 문자열의 모든 값이 알파벳인 경우 true를 리턴한다. (a-z, A-Z)

3. isdigit() - 문자열의 모든 값이 숫자인 경우 true를 리턴한다. (0-9)

4. islower() - 문자열의 모든 값이 소문자인 경우 true를 리턴한다. (a-z)

5. isupper() - 문자열의 모든 값이 대문자인 경우 true를 리턴한다. (A-Z)

 

파이썬의 validator 함수는 모든 문자열에 대하여 유효한 경우에만 true를 리턴하지만, 문제에서는 input으로 입력되는 문자열에서 하나의 문자만 일치하여도 true를 리턴하도록 하였다.

 

문제풀이

문자열에서 일치하는 하나의 문자가 있는지를 찾으면 되므로, bool타입의 변수를 false로 셋팅하고 for문을 이용하여 일치하는 문자가 있을 경우 true로 변경하는 방법으로 하였다. 

if __name__ == '__main__':
    s = input()  
    
    isalnum = False #alphanumeric 
    isalpha = False #alphabetical 
    isdigit = False #digits
    islower = False #lowercase 
    isupper = False #uppercase 
    
    for i in range(len(s)): 
        if s[i].isalnum(): 
            isalnum = True
        if s[i].isalpha():
            isalpha = True
        if s[i].isdigit():
            isdigit = True
        if s[i].islower():
            islower = True
        if s[i].isupper():
            isupper = True        
       
    print("{0}\n{1}\n{2}\n{3}\n{4}".format(isalnum, isalpha, isdigit, islower, isupper))

Discussions

아래처럼 any를 사용하는 방법이 많았는데 나는 컴파일이 되지 않았다. python2에서는 정상적으로 작동하였다.

if __name__ == '__main__':
    str = raw_input()  
    
    print any(c.isalnum() for c in str)
    print any(c.isalpha() for c in str)
    print any(c.isdigit() for c in str)
    print any(c.islower() for c in str)
    print any(c.isupper() for c in str)

 

728x90
LIST
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함