Course 2.2Lesson 3 of 5
The + sign joins two strings and the * sign repeats a string. A number must be changed to text with str() before it is joined with +. An f-string puts values inside text: write f before the opening quotation mark and put the names or sums in curly brackets. After a colon inside the brackets we can say how to show the value: :.2f gives two decimal places, and :>8, :<8 and :^8 align the text in a width of 8.
Try the code and the simulations beside the lesson.
After this lesson you can
You met these in the earlier lessons. Now we look at them carefully.
A string and a number cannot be joined with +. "Age: " + 12 is a TypeError. You must write "Age: " + str(12). This is easy to forget, and it is the reason why we need a better tool.
Each coloured bar is one copy of the piece. + would put two texts side by side in the same way.
An f-string puts the values of variables and sums right inside the text. You write the letter f before the opening quotation mark. Inside the text, a name or a sum in curly brackets { } is replaced by its value.
You do not need str(). An f-string changes the numbers into text by itself. So it is shorter and easier to read than joining with +.
After a colon inside the curly brackets, you can say how to show the value. The three most useful forms:
| Written | Meaning | Example | Shows |
|---|---|---|---|
{price:.2f} | two decimal places | {49.5:.2f} | 49.50 |
{x:>8} | right aligned in 8 characters | {"ab":>8} | ab |
{x:<8} | left aligned in 8 characters | {"ab":<8} | ab |
{x:^8} | centred in 8 characters | {"ab":^8} | ab |
The last lines of the program show the three alignments, inside square brackets so that you can see the spaces. The f-string with .2f is the best way to show money: 49.5 becomes 49.50.
With alignment, you can make a neat table. Imagine three lines such as f"{name:<10}{marks:>5}". The names start in the same column, and the marks end in the same column. Programs that print reports use this all the time.
f. Without it, "Hello {name}" shows the curly brackets as they are.f"... {d["a"]} ..." the inner double marks end the string too soon. Use single marks inside: {d['a']}.+ and no str(). Use an f-string, or a comma in print()."Hello" + name has no space. In an f-string, write the space: f"Hello {name}"..2f without the colon. It is {price:.2f}, with a colon.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
+ joins strings exactly, with no space. * repeats a string.f"... {name} ...", puts values and sums inside text, with no str().{x:.2f} gives two decimal places. {x:>8}, {x:<8} and {x:^8} align text.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
"ab" * 3 + "c"?Asha is 12 with the variables name and age.7.5 as 7.50?"Age: " + 12 an error?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.