본문 바로가기

Python

[Python/level 0] 특정 문자 제거하기 - 120826, replace()

문제 설명

문자열 my_string과 문자 letter이 매개변수로 주어집니다. my_string에서 letter를 제거한 문자열을 return하도록 solution 함수를 완성해주세요.

 

replace()

  • The replace() method replaces a specified phrase with another specified phrase.
    지정된 구문을 다른 구문으로 대체합니다.
  • string.replace(oldvalue, newvalue, count)
Parameter Description
oldvalue Required. The string to search for
newvalue Required. The string to replace the old value with
count Optional. A number specifying how many occurrences of the old value you want to replace. Default is all occurrences

 

code - 1차

def solution(my_string, letter):
    result = ''
    for i in my_string:
        if i not in letter:
            result += i
    return result
  • for문 → if문 → result += i
    • my_string을 i 한 자리씩 for문을 돌립니다.
    • i가 letter에 없는 값이라면, result에 i를 추가합니다.

 

code - 2차 

def solution(my_string, letter):
    return (my_string.replace(letter, ''))
  • replace
    • my_string에서 letter의 값을 ''(빈 문자열)로 바꿉니다.

https://github.com/seonmin5/codingtest_Python
 

GitHub - seonmin5/codingtest_Python

Contribute to seonmin5/codingtest_Python development by creating an account on GitHub.

github.com

 

반응형