이를 위해 숫자를 키로 갖고 단어 표현을 값으로 갖는 사전 객체를 사용합시다 -
dct={'0':'zero','1':'one','2':'two','3':'three','4':'four',
'5':'five','6':'six','7':'seven','8':'eight','9':'nine' 새 문자열 개체 초기화
newstr=''
for 루프를 사용하면 isdigit() 함수의 도움으로 숫자인지 확인할 때 입력 문자열의 각 문자 ch를 탐색합니다.
숫자이면 키로 사용하고 사전에서 해당 값을 찾아 newstr에 추가합니다. 그렇지 않은 경우 ch 자체를 newstr에 추가합니다. 완전한 코드는 다음과 같습니다:
string='I have 3 Networking books, 0 Database books, and 8 Programming books.'
dct={'0':'zero','1':'one','2':'two','3':'three','4':'four',
'5':'five','6':'six','7':'seven','8':'eight','9':'nine'}
newstr=''
for ch in string:
if ch.isdigit()==True:
dw=dct[ch]
newstr=newstr+dw
else:
newstr=newstr+ch
print (newstr) 출력은 원하는 대로
I have three Networking books, zero Database books, and eight Programming books.