大家好。 你們好嗎? 我希望你們一切都好。 在本教程中,我們將了解 如何計算字符串中的單詞 Python. 所以不要浪費時間讓我們了解一下。
內容目錄
如何在python中計算字符串中的單詞
- 在python中計算字符串中的單詞
在python中計算字符串中的單詞 使用分隔符. 通過使用分隔符,您可以在 python 中計算字符串中的單詞。 這是非常容易使用。 讓我們通過以下示例了解這一點:
mylist=”pizza, burger, panipuri, momos, frenky” result = len(mylist.split(‘,’)) print(mylist.split(‘,’)) print(“There are ” + str(result) + ” words.”)
輸出 :
[‘pizza’, ‘ burger’, ‘ panipuri’, ‘ momos’, ‘ frenky’] There are 5 words. - 如何在python中計算字符串中的單詞
在python中計算字符串中的單詞 使用 len(). 通過使用 len() 您可以在 python 中計算字符串中的單詞。 這是非常容易使用。 讓我們通過以下示例了解這一點: mylist=”pizza, burger, panipuri, momos, frenky” result = len(mylist.split()) print(“There are ” + str(result) + ” words.”) 輸出 :
There are 5 words. - python計算單詞
在python中計算字符串中的單詞 使用 len(). 通過使用 len() 您可以在 python 中計算字符串中的單詞。 這是非常容易使用。 讓我們通過以下示例了解這一點: mylist=”pizza, burger, panipuri, momos, frenky” result = len(mylist.split()) print(“There are ” + str(result) + ” words.”) 輸出 :
There are 5 words.
方法一:使用分隔符
通過使用分隔符,您可以計算字符串中的單詞。 這是非常容易使用。 讓我們通過以下示例了解這一點:
mylist="pizza, burger, panipuri, momos, frenky" result = len(mylist.split(',')) print(mylist.split(',')) print("There are " + str(result) + " words.")
輸出 :
['pizza', ' burger', ' panipuri', ' momos', ' frenky'] There are 5 words.
方法二:使用 len()
通過使用 len() 您可以計算單詞。 這是非常容易使用。 讓我們通過以下示例了解這一點:
mylist="pizza, burger, panipuri, momos, frenky" result = len(mylist.split()) print("There are " + str(result) + " words.")
輸出 :
There are 5 words.
方法三:使用正則表達式模塊
通過使用 RegEx 模塊,您可以計算單詞。 這是非常容易使用。 讓我們通過以下示例了解這一點:
import re text="pizza, burger, panipuri, momos, frenky" result = len(re.findall(r'w+', text)) print("There are " + str(result) + " words.")
輸出 :
There are 5 words.
結論
這都是關於本教程的。 希望所有方法對你有很大幫助。 在下面評論您的想法和疑問。 另外,請在下面評論哪種方法對您有用?