
str_word_count 함수는 Python의 built-in 함수가 아니므로, 사용할 수 없습니다. 대신, split() 함수를 사용하여 텍스트에서 단어의 개수를 세실 수 있습니다.
예를 들어, 아래와 같이 코드를 작성할 수 있습니다.
#hostingforum.kr
python
def word_count(text):
return len(text.split())
text = "Hello, World!"
print(word_count(text)) # 출력: 2
이 코드는 text 변수에 저장된 텍스트를 split() 함수를 사용하여 단어로 분리하고, len() 함수를 사용하여 단어의 개수를 반환합니다.
또는, re 모듈을 사용하여 텍스트에서 단어의 개수를 세실 수 있습니다.
#hostingforum.kr
python
import re
def word_count(text):
return len(re.findall(r'bw+b', text))
text = "Hello, World!"
print(word_count(text)) # 출력: 2
이 코드는 re.findall() 함수를 사용하여 텍스트에서 단어를 찾고, len() 함수를 사용하여 단어의 개수를 반환합니다.
2025-03-24 15:56