문자열 s의 길이가 4 혹은 6이고, 숫자로만 구성돼있는지 확인해주는 함수, solution을 완성하세요. 예를 들어 s가 "a234"이면 False를 리턴하고 "1234"라면 True를 리턴하면 됩니다.
.isdigit()
str.isdigit()
- Return True if all characters in the string are digits and there is at least one character, False otherwise.
- Return True: digits(지수, 밑수 등도 포함)이면 True 반환
- Return Flase: 1글자라도 문자가 들어가면 False 반환
- Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.
- Exponents, like ², are also considered to be a digit.
- 참고자료: Python 공식 문서, w3schools
code
def solution(s):
if len(s) == 4 or len(s) == 6:
if s.isdigit():
return True
return False
- 첫번째 if문: 문자열 s의 길이 비교하고 True면 다음 if문 실행
- 두번째 if문: 문자열 s가 digit이면 return True
- 첫번째, 두번째 if문을 모두 통과하지 못하면 return False
https://github.com/seonmin5/codingtest_Python
GitHub - seonmin5/codingtest_Python
Contribute to seonmin5/codingtest_Python development by creating an account on GitHub.
github.com
반응형
'Python' 카테고리의 다른 글
[Python/level 0] 피자 나눠 먹기 (1) - 120814, (n-1) // 7+1 (2) | 2024.07.29 |
---|---|
[Python/Silver IV] 덱 - 10866, deque, split(" ") vs. split(), sys.stdin (0) | 2024.07.29 |
[Python] 모음 제거, .replace(), .join(), 리스트 컴프리헨션 (0) | 2024.07.17 |
[Python] 짝수 홀수 개수, answer = [0, 0], answer[n%2] +=1 (1) | 2024.07.17 |
[Python] 머쓱이 보다 키 큰 사람, .append(), .sort() - 생략 가능 (0) | 2024.07.17 |