Python Programming Challenge #3 - Converting File Names to Headlines

Free Video Tutorial and Guide

Are you looking for a new coding challenge? Test your Python programming skills by trying to convert file names into headlines!

Starting File

Final File

Video Transcription

Python Developer Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

So the idea is we're going to write a function that takes in a list of file names (hyphenated or not, with or without file extensions) and returns a list of headlines such that all words from the file name appear in the headline in the same order, all words except for those found in the junk word list are capitalized, all hyphens (if any) have been replaced by spaces, and the file extension (if it exists) has been eliminated.

To do this, we can define a function called make headlines and it will take a files list as its argument. We'll create a new empty list to store the final product, which is a list of five of headlines. We'll replace hyphens with spaces and make the file name lowercase, then make a list from the file name string.

We know that the first word of the headline should be capitalized even if it is a junk word. We'll loop through the words list and check if it's not in the junk words. If it's not, we'll capitalize it. Finally, we'll start the headline string and loop through the words list again to build the headline.

If the current word is not one of the junk words, it gets capitalized. We've already capitalized the first word. If the junk word starts the phrase, once you're done with the loop, it gets added to the headline with a space. To capitalize the non-chunk word, if the current word is not a junk word, we need to check if there is a dot.

If there is no dot in the headline, we append the headline with no changes, and strip the ending space of the string. If there is a dot, we append a slice of the headline from the beginning up to but not including the dot, thereby getting rid of the file extension. We then return the headlines list and print a call to the function with the news picks list as an argument. We have seven string methods and one list method. This solution has 16 lines but can be refactored to make it more flexible.

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram