β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 print('1+2')
becomes: '1+2'
.
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!β )
String Methods
Built-in actions you can use on string data.
For example:
name = "sk"
print(name.upper())
message = "WHY ARE YOU YELLING!"
print(message.lower())
book = "to kill a mockingbird"
print(book.title())
.upper()
.title()
.lower()
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?
Practice!
π Go to repl!
Organization
Separate each exercise with a line space and add a comment above each to tell me what you are doing.
Grading Policy
- 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:
print("Languages:\n\Python\nC\nJavaScript")
Creates this β>
Languages:
Python
C
JavaScript
Option 1: Utilize \n
and \t
to create a shopping list with just one print statement.
Option 2: Utilize \n
and \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.