Activity 2-3

String Formatting Lab

You and your best friends just came back from watching Kung Fu Panda! You obviously had an amazing time and now you want to see what your friends thought about the movie.

Write a program that asks questions about the movie, manipulates/analyzes their answers, and prints the data in a nicely formatted way. In this activity, we recommand trying out commands by running them through the interactive python terminal and then implementing them within your program.

At the end of class, if you are done, please check this activity off with the TAs. Otherwise, please share your program with cs0030handin@gmail.com by the end of the day.

Task 1

Use the input() function to prompt questions asking the user for:

  • First Name
  • Last Name
  • Describe the experience in a word
  • Describe the experience in a sentence
  • Rate the experience out of 10

Store all of the information in variables.


Task 2

When taking in survey responses, it is common for users to enter responses in a non formatted way. For example, some users may enter their name as ‘john’ or ‘JOHN’ or ‘John’. Use the capitalize() function to modify the user inputs for first and last name into a standard form. In this case, that capitalize() function will change all forms of john into "John".

Example:
Input: "shifu"
Function: "shifu".capitalize()
Output: "Shifu"

Task 3

There are other functions that python allows you to perform on strings. Let’s use the upper() function to capitalize all of the letters in the user’s one word description of Kung Fu Panda.

Example:
Input: "awesome"
Function: "awesome".upper()
Output: "AWESOME"

Task 4

Similar to the upper() function, python has a lower() function that lowercases all of the characters in a string. Use lower() to lowercase all of the letters in the sentence.

Example:
Input: "That was a fantastic Movie!"
Function: "That was a fantastic Movie!".lower()
Output: "that was a fantastic movie!"

Congratulations for making it this far in the activity! Let's test our code to make sure it is working the way we want so far. Run the program and use print() to print the value of all of the variables to make sure they are formatted correctly. When writing large programs, it is good practice to print out variables incrementally to make sure the program is working correctly.


Task 5

Task 5 should be completed by running python interactively in the shell. To do this, open a new tab in terminal or a new window and type in Python3. This should begin the interactive mode.

One of the benefits of using the upper() and lower() functions is that it allows programmers to standardize strings for comparison. The '==' operator tests for equivalence. If the strings are equal, the program should print out True, otherwise False. Use print lines to test out the following:

  • print(“Awesome” == “awesome”)
  • print(“AWESOME” == “awesome”)
  • print(“awesome” == “awesome”)
  • print(“AWESOME” == “AWESOME”)

As you can see, only the third and fourth statements return True. Using the lower() and upper() function allows us to make comparisons between strings. Try using both print statements listed below.

  • print("Awesome" == "AWESOME")
  • print("Awesome".lower() == "AWESOME".lower())
  • print("awEsOmE" == "AWeSoMe")
  • print("awEsOmE".upper() == "AWeSoMe".upper())

Task 6

We will attempt to classify the user's sentence review of the movie as either good or bad. Let’s implement a simple approach to this by seeing if the word ‘awesome’ is in the sentence. If the word awesome is in the sentence, then that means the review was good, if the word awesome is not in the sentence then that means the review is bad. This approach of finding good or bad words to classify a text is called sentiment analysis.

  1. Lowercase all of the words in the sentence using lower()
  2. Use the ‘in’ syntax to see if the word ‘awesome’ is in the sentence
Without using the lower function:
Input: "That was an Awesome movie!"
Function: "awesome" in "That was an Awesome movie"
Output: False
Using the lower function:
Input: "that was an awesome movie!"
Function: "awesome" in "that was an awesome movie"
Output: True

There are other words that can classify a sentence as a good review. We can add them in using the ‘or’ syntax. This is obviously a very poor classification method, but the point here is that the lower() function can be an effective way to compare strings.

Run the program again and make sure that the sentiment analysis works.


Task 7

Task 7 should be completed by running python interactively in the shell. Go back to the terminal window that is in interactive mode.

Another interesting function is find() which returns the location of where a word or character is within a string. But first, the find() function needs to know what word to find! Pass the string you want to find into find() as a parameter. For example calling sentence.lower().find(“awesome”) would return the location of the word awesome within the sentence. Try it out! (Remember, in Python strings and arrays are 0 indexed.)


Task 8

We have come a long way! Now that we have all of our information standardized, lets put it all together! We want to do the following:

Example:
Input: "master" "shifu" "awesome" "That movie was fantastic" "10"
Output: Master Shifu described the movie as AWESOME, specifically a 10 out of 10. This was a positive review: True. That movie was fantastic.

To do this we will need to create a standard string and input words within the string. The following implements the str.format() function. This is a cleaner way of printing out a string than string concatentation. There are a few ways to do this. Try out all three and run your code to test for functionality.

Method 1:

This method is very useful for building and printing out relatively short strings. All of the variables are passed into the format() function and are placed into the {} within the string based on order.

"{} {} described the movie as {}, specifically a {} out of 10. This was a positive review: {}. {}".format(first_name, last_name, word, number, classification, sentence)

Method 2: By Index

This method is useful for longer strings, and strings where a variable is inserted in more than once. In this method, an index is placed within the {} characters, to refer to the variable pased into .format().

"{0} {1} described the movie as {2}, specifically a {3} out of 10. This was a positive review: {4}. {5}".format(first_name, last_name, word, number, classification, sentence)

Method 3: Parameterized Input

This method is useful for even larger strings. Every variable is associated with a name that can be used instead of an index.

"{f_name} {l_name} described the movie as {word}, specifically a {rating} out of 10. This was a positive review: {classification}. {sentence}".format(f_name = first_name, l_name = last_name, word = word, rating = number, classification = classification, sentence = sentence)

Run your code to make sure that it works.


Task 9

Sometimes users can get carried away when writing a sentence description and maybe end up writing a paragraph. If we want our final output to be concise, we can truncate the size of the string. Use the following syntax to truncate the size of the sentence to 20 characters.

"{} {} described the movie as {}, specifically a {} out of 10. This was a positive review: {}. {:.20}".format(first_name, last_name, word, number, classification, sentence)


Task 10

Congratulations, you have finished the lab! If there is still time, click here to learn more about formatting strings. Spend some time playing around with the functions listed on the website. We recommend doing this in the interactive python terminal.


Once you're done, please check off your lab with a TA or share your file with cs0030handin@gmail.com by midnight, 2/28.