Formatted Strings
In previous lessons, we used simple strings, but we want our programs to work smarter and be more dynamic. Imagine you own an online store and need to display a user's name, their shopping cart, or some other piece of information.
It is not practical to code a separate string for each user. To make our code more dynamic, we can create variables, such as name = 'Kieran', and then use a formatted string.
Previously, we learned that to print a string we would use print('Hi' + ' ' + name), which displays Hi Kieran. But perhaps we want to use more user information, for example, age = 8. Writing print('Hi' + ' ' + name + ', you are ' + str(age) + ' ' + 'years old.') starts to get cumbersome.
name = "Kieran"
age = 8
print('Hi' + ' ' + name)
print('Hi' + ' ' + name + ', you are ' + str(age) + ' ' + 'years old.')
Hi Kieran
Hi Kieran, you are 8 years old.
There is a better way of doing this using formatted strings:
name = "Kieran"
age = 8
print(f'Hi {name}. You are {age} years old.')
Hi Kieran, you are 8 years old.
Prior to Python 3.6
Prior to Python 3.6, the same could be accomplished (and still works) by using the .format() method:
name = "Kieran"
age = 8
print('Hi {}. You are {} years old'.format(name, age))
Hi Kieran, you are 8 years old.
In this example, the default order is from left to right, but we can use any order we want by using the index "An index is the numerical position of an element within an ordered collection (such as a list, tuple, array, or string). " of the variables. For example: print('Hi {1}. You are {0} years old'.format(name, age))
This will print: Hi 8. You are Kieran years old. Just remember that in programming languages, we begin counting at 0.
name = "Kieran"
age = 8
print('Hi {1}. You are {0} years old.'.format(name, age))
Hi 8. You are Kieran years old.
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named f-strings.py in your part13 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: The Personal Greeting
Objective: Create a basic f-string.
- Create a variable called first_name and assign it your name.
- Create a variable called favorite_language and assign it "Python".
- Use an f-string to print a message that says: Hi, my name is [first_name] and I am learning [favorite_language].
Exercise 2: The Scoreboard
Objective: Handle multiple data types (strings and integers) without manual conversion.
- Create three variables: player_name, level, and score.
- Assign them any values you like (e.g., "Ash", 5, '1200').
- Use an f-string to display: Player: [player_name] | Level: [level] | Score: '[score]'
Exercise 3: The Old School Method
Objective: Practice the .format() method.
- Create a variable city = "London" and weather = "sunny".
- Use the .format() method (without indexes) to print: The weather in London is currently sunny.
Exercise 4: The Index Swap
Objective: Master zero-based indexing and manual ordering using the .format() method.
Step 1: Create five variables for your favorite fruits:
- fruit1 = "Apples"
- fruit2 = "Oranges"
- fruit3 = "Bananas"
- fruit4 = "Mangoes"
- fruit5 = "Strawberries"
Step 2: Use a single print() statement with the .format() method. Place the variables inside .format() in order from 1 to 5.
Step 3: Use the indexes inside the curly braces {} to rearrange the output. Your goal is to print your list from highest to lowest preference, even though the variables are listed 1 through 5 in the code.
Example Goal: My favorite fruits from highest to lowest are: Mangoes, Strawberries, Oranges, Bananas, and Apples.
Exercise 5: Mathematical f-strings
Objective: Learn that f-strings can perform logic inside the braces.
- Create two integer variables: num1 = 10 and num2 = 5.
- Use an f-string to print the result of adding them together inside the curly braces.
- Output should look like: The sum of 10 and 5 is 15.
Don't Forget to commit and Push!