Super Simple Python: Word Count

Super Simple Python is a series of Python projects you can do in under 15 minutes. In this episode, we’ll be covering how to get the word count from a text in under 5 lines of code!

Video version:

So far in the Super Simple Python series, we’ve mostly covered programs using the random library like the Password Generator, or building and using functions like the Simple Calculator or Generating a Card Deck. This episode is going to be on building and using a function. The text we’ll be using is taken from this post on Agrivoltaics, the combination of agriculture and solar panels.

Test Text

text = “More broadly, agrivoltaic systems create a number of dual-use synergies between the solar panels and agricultural crops, which unlocks benefits for all aspects of the system: solar panel PV performance, crop yields, and economic benefits for the farmer. Solar PV panel performance increases for a number of reasons. Firstly, because the panels are placed above the crops, this means they are farther off the ground (and closer to the sun) so they will have increased efficiency of power generation. Secondly, because the land under the solar panel is now cropland, it stays cooler and prevents the ground under the solar panel from heating up too much, which in turn increases the efficiency of the solar panel. Agrivoltaic systems also generate significant benefits for crop yield. The solar panels can function as a protective foil, decreasing the destructive impacts of weather and wind on crop yield. Furthermore, the solar panels can be placed in a way to optimize light availability for the crops. Finally, agrivoltaic systems introduce a number of direct economic benefits for farmers (aside from the cheap irrigation process described above). The presence of the solar panel can actually reduce unnecessary wind and solar radiation impacts on crops, which enables them to better retain water, which reduces the need for irrigation for farmers. The solar panels can also collect rainwater, which can further decrease irrigation costs. Finally, the presence of solar panels will increase property values for the land, which could enable the farmer to sell for a higher value. All of these benefits showcase the dual-use synergies between solar PV panels and agricultural operations present within an agrivoltaic system. In fact, pilot projects have shown substantial land use efficiency increases as a result of installing agrivoltaic systems (ranging from 60% to 186% increase) There is no doubt that we must scale up this application here in the US.”

https://www.greenshareenergy.com/blog/what-are-agrivoltaics

Counting the Words

We don’t have any imports to deal with in this program. Let’s start off by creating our function, word_count. The function will take one input, the text, which we expect to be a string. In the function, we will use the split function of strings to split the input string on spaces. We’ll save the split string into a list and return the length of the list. This should give us the number of words in a text.

def word_count(text: str):
    _list = text.split(" ")
    return len(_list)
 
print(word_count(text))

Finally, we run our program and we should see the number of words within the passed in text.

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

%d bloggers like this: