search
HomeBackend DevelopmentPython TutorialExample of counting the number of words in text using the len function in Python

Example of counting the number of words in text using the len function in Python

Application example of len function in Python: How to use it to count the number of words in text

In Python programming, the len function is a very useful function. Used to return the length or number of elements of an object. In this article, we will introduce how to use the len function to count the number of words in text and provide specific code examples.

Before you start writing code, you need to understand how to define a word. In this article, we will use spaces as word separators, that is, any string between two spaces is considered a word.

The following is a simple code example that shows how to use the len function to count the number of words in text:

def count_words(text):
    word_count = len(text.split())
    return word_count

text = "Python是一种简单易学但功能强大的编程语言。它被广泛应用于各种领域,包括软件开发、数据分析和人工智能等。"

word_count = count_words(text)
print("文本中的单词数量为:", word_count)

In the above code, a function named count_words is first defined, It receives a string parameter text. The implementation idea of ​​the function is to split the text string text into words one by one through the split method, and use the len function to obtain the length of the word list, that is, the number of words. Finally, the number of words is returned.

In the sample code, we assign a text containing multiple sentences to the variable text, then call the count_words function to count the number of words in the text, and print the results.

Run the above code and the number of words in the output text will be: 17. This is because there are 17 space-separated strings in the text, which make up 17 words.

Of course, in actual applications, you may need to perform some preprocessing on the text, such as removing punctuation marks and line breaks, etc. The following is an improved code example that shows how to preprocess the text before counting the number of words:

import re

def count_words(text):
    text = re.sub(r'[^ws]', '', text)  # 去除标点符号
    text = text.replace('
', '')  # 去除换行符
    word_count = len(text.split())
    return word_count

text = """Python是一种简单易学但功能强大的编程语言。
它被广泛应用于各种领域,包括软件开发、数据分析和人工智能等。"""

word_count = count_words(text)
print("文本中的单词数量为:", word_count)

In the improved code, we first replaced all punctuation marks with regular expressions, using re. sub method is implemented. Then, the newlines were removed using the .replace method. Finally, count the number of words according to the previous method.

Run the improved code and the number of words in the output text will be: 18. By removing punctuation and line breaks, we cleaned up the text and got a more accurate word count.

To summarize, the above is an example of using the len function to count the number of words in the text, splitting the string into words by the split method, and using the len function to obtain the length of the word list. In practical applications, you can perform text preprocessing as needed to obtain more accurate results. I hope this article will help you understand and use the len function!

The above is the detailed content of Example of counting the number of words in text using the len function in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python: A Deep Dive into Compilation and InterpretationPython: A Deep Dive into Compilation and InterpretationMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Is Python an interpreted or a compiled language, and why does it matter?Is Python an interpreted or a compiled language, and why does it matter?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

For Loop vs While Loop in Python: Key Differences ExplainedFor Loop vs While Loop in Python: Key Differences ExplainedMay 12, 2025 am 12:08 AM

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

For and While loops: a practical guideFor and While loops: a practical guideMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

Python: Is it Truly Interpreted? Debunking the MythsPython: Is it Truly Interpreted? Debunking the MythsMay 12, 2025 am 12:05 AM

Pythonisnotpurelyinterpreted;itusesahybridapproachofbytecodecompilationandruntimeinterpretation.1)Pythoncompilessourcecodeintobytecode,whichisthenexecutedbythePythonVirtualMachine(PVM).2)Thisprocessallowsforrapiddevelopmentbutcanimpactperformance,req

Python concatenate lists with same elementPython concatenate lists with same elementMay 11, 2025 am 12:08 AM

ToconcatenatelistsinPythonwiththesameelements,use:1)the operatortokeepduplicates,2)asettoremoveduplicates,or3)listcomprehensionforcontroloverduplicates,eachmethodhasdifferentperformanceandorderimplications.

Interpreted vs Compiled Languages: Python's PlaceInterpreted vs Compiled Languages: Python's PlaceMay 11, 2025 am 12:07 AM

Pythonisaninterpretedlanguage,offeringeaseofuseandflexibilitybutfacingperformancelimitationsincriticalapplications.1)InterpretedlanguageslikePythonexecuteline-by-line,allowingimmediatefeedbackandrapidprototyping.2)CompiledlanguageslikeC/C transformt

For and While loops: when do you use each in python?For and While loops: when do you use each in python?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.