❝String Methods❞November 23, 2016
🎯 Learning Target
I can write strings and utilize string methods in Python
Do Now (in Google Classroom)
# What prints if you run this code? print("2+1") print("2+1") + 2 print("2+1") * 2
Strings & Things
Strings are any characters inside quotes.
print("This is a string with double quotes") print('This is a string with single quotes') # doesn't matter which quotes you use as long as you are consistent.
Python can’t evaluate anything inside of a string, so
What is the proper way to print the statement below:
It’s a Wonderful Life!
print( “It’s a Wonderful Life!’ ) print ( ‘It’s a Wonderful Life!’ ) print ( It’s a Wonderful Life! ) print ( “It’s a Wonderful Life!” )
Built-in actions you can use on string data.
name = "sk" print(name.upper()) message = "WHY ARE YOU YELLING!" print(message.lower()) book = "to kill a mockingbird" print(book.title())
We can also add strings together. In Python, this is called concatenation:
first_name = 'grace' last_name = 'hopper' name = first_name.title() + last_name.title() print(name) # What's the mistake?
👉 Go to repl!
Separate each exercise with a line space and add a comment above each to tell me what you are doing.
- 2 ⭐, 1 ⭐⭐ = 80
- 2 ⭐, 2 ⭐⭐ = 85
- 2 ⭐, 2 ⭐⭐, 1 ⭐⭐⭐ = 90
- 2 ⭐, 2 ⭐⭐, 2 ⭐⭐ = 100
Option 1: Store a person’s name in a variable. Print the name in uppercase, lowercase and title case.
Option 2: Find a quote from a famous person that you admire. Print the quote and the name of it’s author.
Option 1: Find a quote from a famous person that you admire and store it in a variable called message. Store the name of the author in a variable called famous_person. Print the quote and the name of it’s author using concatenation. It should look like:
Steve Jobs: "_____________quote_here_____________".
Option 2: In order to use a tab in your display you need to add \t in front of what you want printed.
For example: This code –>
print("\tThis creates a tab.")
will end up looking like –>
This creates a tab.
Find another quote and print the quote and the author but tab the author’s name on a new line after the quote.
This line of code:
Creates this –>
Option 1: Utilize
\t to create a shopping list with just one print statement.
Option 2: Utilize
\t to recreate the statement in the picture below using just one print statement. Each word must be printed on the same line and in approximately the same position as it appears in the picture.