How to Count Hyphenated Words in Python

Python is an adaptable language that is used for many purposes including text analysis and manipulation. This tutorial guides you on how to count hyphenated words in a text file using Python.

We will read a text file, then split the contents of the file into individual words. Afterward, we will analyze the words and count the number of hyphenated words.

Step 1: Import Necessary Module

First, we need to import the Python modules that will allow us to work with file and text data. For this task, we need os to handle file paths and to check if the file exists, re which enables regular expressions for pattern matching.

Step 2: Load and Read the Text File

Now, we will load the text file and read its content. We will store the contents of the text file in a variable.

Let’s say the content of the file sample.txt is:

Hello-world, I am learning-Python. Python-is really-fun! Here-is another-example.

Step 3: Split the Text into Words

After we load the text, we need to split it into individual words. We will use the split() function to do this.

Step 4: Count the Hyphenated Words

The last step is to count the hyphenated words. We can do this by iterating through the list of words and checking if there is a hyphen in each word.

Put It All Together…

It’s time to put all this code together.

Running the Program

When you run this program with the given text file, you should see this result:

The number of hyphenated words is: 6

Conclusion

Text manipulation is a common task in data analysis and Python offers several reliable and efficient ways to handle it. This tutorial has shown you how to count hyphenated words from a text file in Python.

Hopefully, it has deepened your understanding of text analysis in Python. You may want to try your own text analysis tasks using this code as a foundation.