[프로그래머스 Level1] 문자열 다루기 기본

ham·2022년 5월 21일
0

programmers

목록 보기
19/21
post-thumbnail

  • 문제 설명

    문자열 s의 길이가 4 혹은 6이고, 숫자로만 구성돼있는지 확인해주는 함수, solution을 완성하세요. 예를 들어 s가 "a234"이면 False를 리턴하고 "1234"라면 True를 리턴하면 됩니다.

  • 제한사항

    s는 길이 1 이상, 길이 8 이하인 문자열입니다.

  • 입출력 예

    sresult
    "a234"false
    "1234"true

코드

def solution(s):
    if (len(s) == 4 or len(s) == 6) and s.isdigit() == True:
        answer = True
    else:
        answer = False
    return answer

풀이

  • isdigit()함수를 사용해 문자가 있는지 판별

결과

다른 사람 코드

def alpha_string46(s):
    return s.isdigit() and len(s) in (4, 6)
profile
자라나는 중🌱

0개의 댓글