Posts 핸드폰 번호 가리기 [Python]
Post
Cancel

핸드폰 번호 가리기 [Python]

1. 핸드폰 번호 가리기


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

1.1 문제 설명

  • 프로그래머스 모바일은 개인정보 보호를 위해 고지서를 보낼 때 고객들의 전화번호의 일부를 가립니다.
  • 전화번호가 문자열 phone_number로 주어졌을 때, 전화번호의 뒷 4자리를 제외한 나머지 숫자를 전부 *으로 가린 문자열을 리턴하는 함수, solution을 완성해주세요.


1.2 제한 조건

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


1.3 Solution

1
2
3
4
5
def solution(phone_number):
    answer = phone_number.replace(phone_number[:-4], "*" * len(phone_number[:-4]))
    return answer

solution('01011111111')
1
'*******1111'
  • replace와 list의 offset index를 사용하여 *로 변경하는 함수를 작성
This post is licensed under CC BY 4.0 by the author.