본문 바로가기

Programming/Python

[Python] True 또는 Flase로 답하는 함수

 

1. 숫자인가?

st1 = "123"
st2 = "OneTwoThree"

st1.isdigit()
True

st2.isdigit()
False

2. 문자인가?

st1.isalpha()
False

st2.isalpha()
True

3. 특정 문자로 시작하거나 끝나는가?

# 특정 문자로 시작하는가?
str = "Supersprint"
str.startswith("Super")
True

# 특정 문자로 끝나는가?
str.endswith("int")
True

4. 활용 예시

def main():
    pnum = input("스마트폰 번호 입력: ")
    if pnum.isdigit() and pnum.startswith("010"):
        print("정상적인 입력입니다.")
    else:
        print("정상적이지 않은 입력입니다.")
        
main()

스마트폰 번호 입력: 01012344444
정상적인 입력입니다.

스마트폰 번호 입력: 010fdf33
정상적이지 않은 입력입니다.

'Programming > Python' 카테고리의 다른 글

[Python] 예외처리  (0) 2020.12.30
[Python] 모듈  (0) 2020.12.29
[Python] if문  (0) 2020.12.27
[Python] for 문  (0) 2020.12.27
[Python] 리스트 값 삭제하기  (0) 2020.12.26