String Formatting

1.10. String Formatting#

Often it’s nice to format the information you want to display to the user.

For example, you might use string concatenation.

age = "10"
print("You are " + age + " years old")

However, you will notice that age in the above example is a string. This won’t work the same way if age is an integer.

age = 10
print("You are " + age + " years old")

Remember, you can’t mix and match types!

1.10.1. Using .format()#

We can format strings using .format(). Here is an example:

age = 10
print("You are {} years old".format(age))
  • .format() comes after the last '.

  • The information we want to display goes into the ().

  • {} is a placeholder. The information will be placed where the {} is in the string.

../../_images/format1.png

We can format multiple pieces of information! Each piece of information is separated by a , and unpacked into their respective {} placeholders.

name = "Alice"
age = 10
print("Hi {}, you are {} years old".format(name, age))
  • Each piece of information we want to display goes into the ().

  • {} are placeholders. The information will be placed in order where the {}’s are in the string.

../../_images/format2.png

We are also able to format floats to a specific number of decimal places. Instead of using an empty {} placeholder, we can use {:.xf} to display the first x decimal places (you can remember .x means ‘x digits after the decimal point’ and the f means that you are formatting a float). For example, {:.2f} will display to 2 decimal places and {:.5f} will display to the first 5 decimal places.

pi = 3.14159265359
print("Pi to 2 decimal places: {:.2f}".format(pi))
print("Pi to 5 decimal places: {:.5f}".format(pi))
print("Pi to 10 decimal places: {:.10f}".format(pi))
Question 1

What do you think the output of the following code will be?

activity = 'programming'
print('I love {}!'.format(activity))
Solution
I love programming!

The string stored in the variable activity will fill the placeholder {} to construct the string 'I love programming!'.

Question 2

What do you think the output of the following code will be?

print('{} and {} make twenty'.format('ten', 10))
Solution

Solution is locked

Question 3

What do you think the output of the following code will be?

print('1/3 is approximately {:.2f}'.format(1/3))
Solution

Solution is locked

Code challenge: Nice to Meet You!

Write a program that reads in a user’s name and prints out

Hello name.
Nice to meet you!

Here are some examples of how your code should run.

Example 1

Enter your name: Jess
Hello Jess.
Nice to meet you!

Example 2

Enter your name: Ali
Hello Ali.
Nice to meet you!

Hint

Don’t forget to take note of the punctuation and the capitalisation. You need to match the spelling exactly!

Solution

Solution is locked

Code challenge: Round to 2 Decimal Places

Write a program that reads in a number from the user and outputs the number to 2 decimal places.

Here are some examples of how your code should run.

Example 1

Enter a number: 1234.5678
Your number to 2 decimal places is: 1234.57

Example 2

Enter a number: 3.14159
Your number to 2 decimal places is: 3.14
Solution

Solution is locked

Code challenge: Seconds in Day

Write a program to calculate the number of seconds in a specified number of days.

Here are some examples of how your code should run.

Example 1

How many days? 1
There are 86400 seconds in 1 days.

Example 2

How many days? 5
There are 432000 seconds in 5 days.

Hint

There are 24 hours in 1 day, 60 minutes in 1 hour and 60 seconds in 1 minute. This means there are 24 x 60 x 60 seconds in a day!

Solution

Solution is locked