Python replace()

Happy_JG·2023년 6월 20일

Python

목록 보기
1/12
post-thumbnail

문제

입력 'one2three4fivesix7'
출력 '1234567'

프로그래머스 기초 문제를 풀다가 답안과 해설을 보게되었다.
dictionary로 문제를 해결하다 뭔가 잘못됐다 싶었다.
답안은 replace함수를 사용했다.

quest = 'one2three4fivesix7'
dic = {'one':'1','two':'2','three':'3','four':'4','five':'5','six':'6','seven':'7','eight':'8','nine':'9'}
for key,value in dic.items():
    quest = quest.replace(key,value)
print(quest)

replace함수를 많이 쓰면 안되겠지만 너무 간단하고 직관적 ㅠㅠ

profile
hello!

0개의 댓글