replace2 [프로그래머스/Python] 숫자 문자열과 영단어 [프로그래머스/Python] 숫자 문자열과 영단어 ● 문제 숫자의 일부 자릿수가 영단어로 바뀌어졌거나, 혹은 바뀌지 않고 그대로인 문자열 s가 매개변수로 주어집니다. s가 의미하는 원래 숫자를 return 하도록 solution 함수를 완성해주세요. ● 소스코드 def solution(s): answer = 0 dict = { 'zero' : '0', 'one' : '1', 'two' : '2', 'three' : '3', 'four' : '4', 'five' : '5', 'six' : '6', 'seven' : '7', 'eight' : '8', 'nine' : '9' } for i in dict.keys(): s = s.replace(i,dict[i]) answer = int(s) return ans.. 2021. 10. 11. [Python] "replace", 문자열 치환하기 [Python] "replace", 문자열 치환하기 ● 내용 python에서 문자열에서 특정 구문을 다른 구문으로 치환 할 수 있다. replace 함수를 사용한다. # 사용법 # replace(바꾸고 싶은 문자열, 대체할 문자열) old_str = "개구리가 폴짝폴짝" result = old_str.replace("개구리", "두꺼비") # result = "두꺼비가 폴짝폴짝" result = old_str.replace("폴짝", "개굴") # result = "개구리가 개굴개굴" 2021. 10. 11. 이전 1 다음