共計 189 個字符,預計需要花費 1 分鐘才能閱讀完成。
可以使用 Python 的 split() 方法將字符串分割成單詞,并使用 len() 函數統計單詞個數。
例如,下面的代碼可以統計字符串 s 中單詞的個數:
s =“Hello world! This is a sentence.”
words = s.split()
num_words = len(words)
print(“單詞個數:”, num_words)
輸出結果為:
單詞個數:7
丸趣 TV 網 – 提供最優質的資源集合!
正文完