From the course: Advanced Python

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution: String processing

Solution: String processing - Python Tutorial

From the course: Advanced Python

Solution: String processing

- [Instructor] Let's take a look at my solution for this challenge. So our code needed to process a string and look for a given search term and return a dictionary containing information about the string. So my code starts off by importing the string module, and then I define a result dictionary, and I initialize the values to 0 for the counts, false for the found property, and -1 for the index of the search term. Then I iterate over each character in the string. And for this part, I use the built-in string constants to record information about each of the character types. So each time I come across punctuation, I increase the count, whitespace and so on. For the search term, we needed to find whether the term appeared in the string regardless of case. So to do this, I convert both the source string and the search term to uppercase, and then use the find() function to locate the term in the string. If the result of find is not -1, then the term was found. Otherwise, I leave those two…

Contents