문자열 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
반응형
'Python' 카테고리의 다른 글
[Python/level 0] 특정한 문자를 대문자로 바꾸기 - 181873, replace() (0) | 2024.08.17 |
---|---|
[Python/level 0] 문자 반복 출력하기 - 120825, join() (1) | 2024.08.17 |
[Python/level 0] 최댓값 만들기 (1) - 120847, sort(), sort(reverse=True) (1) | 2024.08.17 |
[Python/level 1] 자연수 뒤집어 배열로 만들기 - 12932, reversed() (1) | 2024.08.17 |
[Python/level 1] 자릿수 더하기 - 12931, map, sum (0) | 2024.08.17 |