Posts 문자열 다루기 기본 [Python]
Post
Cancel

문자열 다루기 기본 [Python]

1. 문자열 다루기 기본


URL : https://programmers.co.kr/learn/courses/30/lessons/12918

1.1 문제 설명

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


1.2 제한 사항

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


1.3 Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def solution(s):
    import re
    import string

    a = (re.findall('[0-9]', s))
    if len(s) >= 8:
        return False
    elif len(s) < 1:
        return False
    elif len(a) != len(s):
        return False
    elif len(a) == 4:
        return True
    elif len(a) == 6:
        return True
    else:
        return False


1
2
s = "a234"
solution(s)
1
False
  • if, elif, else를 이용하여 조건들이 맞으면 True, 아니면 False를 리턴하는 함수를 작성함
This post is licensed under CC BY 4.0 by the author.