fanpin / dsc-1-01-11-strings-lab-online-ds-pt-112618

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduction to Variables: Strings and Booleans - Lab

Introduction

Okay, we have learned about our first data type, the String! Now let's do a little practice with strings. We'll use the methods and functions we introduced in the previous lesson to flex our string-manipulating muscles!

Objectives

You will be able to:

  • Understand and use the data type, Strings
  • Understand, explain and use the correct data types for various types of information (strings)

Instructions

Follow the steps below to manipulate the strings and assign the values to the variables below and get the tests to pass.

1. Below, we have a sentance whose cases are all over the place. Let's normailze the cases and make everything lower case except the first letter in the sentence. hint: there is a string method that does this

sentence = "woW WE LOVE cOdInG and strINGS!"
sentence

2. Next, we have our Flatiron mantra, but it's not in title case like it should be! Let's fix that and use another string method that makes all strings first letter capitalized.

flatiron_mantra = "learn. love. code."
flatiron_mantra

3. The next thing we want to do is practice turning other data types into strings. Below, we have a number 1234, which happens to be our street number in our address, which is a string. So, let's turn that into a string so we can evenutally concatenate it onto our address.

num_to_string = 1234
num_to_string

4. Let's take the num_to_string and add it to the beginning of our street address below. We need to concatenate the variable to the beginning of our string so that we have our full address all in one string and assinged to the variable full_address. hint: None is a placeholder in the below code for you to edit

full_address = None + " Abc street, Hometown USA"
full_address

5. Finally, let's replace some of the characters in a string. Let's say Bart is upset with his family and want's to be adopted by the Flanders family. How would you replace his last name?

name = "Bart Simpson"
name
'Bart Simpson'

Bonus

6. Let's see a new way to print a string with multiple variables in it. Below is code to create two variables and print a concatenation of strings and variables.

item = "apple"
price = "$0.75"

print("The " + item + " costs " + price)
The apple costs $0.75

Here, look up the .format() method to see a new way to print a sentence another way. This method becomes very useful when you have multiple variables in one print statement.

item = "apple"
price = "$0.75"

print() # use the .format() method here

Summary

Great work! In this lab we practiced our skills with strings. We can now manipulate, coerce, and concatenate strings. Remember if there are any things you can't remember or want to discover about code that isn't mentioned in this material, googling is always a great practice!

About

License:Other


Languages

Language:Jupyter Notebook 79.2%Language:Python 20.8%