Week 2 of 105 to 6 hours5 exercises

Week 2: Working with text and numbers

Slice and transform strings, build messages with f-strings, and convert safely between numbers and text.

By the end of this week you can

  • Index and slice strings
  • Use common string methods
  • Build text with f-strings
  • Round and format numbers

Strings are sequences of characters

A string is a row of characters, and every character has a position called an index. Counting starts at 0, so in "python" the letter p is at index 0 and n is at index 5. Negative numbers count from the end: -1 is the last character.

Slicing takes a piece. text[start:stop] gives the characters from start up to, but not including, stop. Leave either side empty to go to the beginning or end. len() tells you how long a string is.

Strings cannot be changed in place. Operations such as upper() give you a new string.

The third number in a slice is the step. A step of -1 walks backwards.

Useful string methods

Methods are actions that belong to a value, written with a dot. A few you will use all the time:

upper() and lower() change the case. strip() removes spaces from both ends. replace(old, new) swaps text. split() breaks a string into a list of words, and "-".join(list) glues a list back into one string. startswith() and endswith() answer yes/no questions, and the keyword in checks whether a smaller piece appears inside a larger string.

Methods can be chained: strip() runs first, then lower() runs on its result.

f-strings: put values inside text

An f-string is a string with an f before the opening quote. Anything inside curly braces is replaced by its value. It is the clearest way to build a message.

Inside the braces you can add a format after a colon. :.2f shows two decimal places, :, adds thousands separators, and :>8 right-aligns in a field 8 characters wide.

Anything that is a valid Python expression can go inside the braces.

Numbers: rounding and converting

round(number, digits) rounds to the digits you ask for. int() cuts off the decimal part (it does not round), and float() makes a decimal. str() turns anything into text so you can join it with other text.

Decimals are stored in binary, so some results look slightly off, such as 0.1 + 0.2 giving 0.30000000000000004. This is normal in every language. Round before you show a decimal to a person.

int(9.99) is 9, not 10. int() always cuts towards zero.

Common mistakes

  • Expecting a slice to include the stop position. word[0:3] stops before index 3.
  • Calling word.upper() and ignoring the result. Strings do not change; you must store the new one: word = word.upper().
  • Joining text and a number with +. "Age: " + 19 is an error. Use str(19) or an f-string.
  • Writing the f before the quote as an afterthought: "{name}" without the f prints the braces as they are.

Quick recap

  • Indexes start at 0; negative indexes count from the end.
  • Slices stop before the stop index.
  • Strings are never changed in place; methods return new strings.
  • f-strings with formats such as :.2f build clean messages.

Practise this week

Work through the 5 exercises for Week 2. Each one tells you which tests pass, and has a hint and a solution if you get stuck.

Open Week 2 exercises

Check your understanding

Pick an answer for each question, then press Check answers.

  1. What does "python"[1:4] give?
  2. Which line prints the price with exactly two decimals, e.g. 45.50?
  3. What is int(7.9)?
  4. name = "asha". After name.upper() on its own line, what is name?
  5. What does "a-b-c".split("-") return?

This course is for learning. The Python engine runs in your browser and your code is never sent to us.