Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Summary

In this lesson, you learned that:

  • Text has methods of its own. A text can't be changed, so a method never changes it: it returns a new text, and the original stays as it was. Store the result or use it right away: name = name.strip().
  • upper() and lower() turn all letters into uppercase or lowercase, and capitalize() makes the first character uppercase and all the others lowercase. To compare without caring about case, turn both texts into lowercase: a.lower() == b.lower().
  • strip() removes the spaces at the start and end of a text, such as those a person typed by accident. Methods can be chained: typed.strip().lower().
  • replace(old, new) replaces every place where old appears. replace(" ", "") removes the spaces.
  • part in text is True if the text contains the part anywhere (in a list, in looks for a whole element). find() returns the index where the part starts, or -1, and count() how many times it appears. startswith() and endswith() check the start and the end of a text. All of them care about case.
  • split() splits a text into a list: at the spaces, or at the separator in the parentheses, "12,7,30".split(","). The parts are texts, so numbers need int().
  • ", ".join(names) joins a list of texts into one text, with the separator between the elements. Only texts can be joined: turn numbers into texts with str() first.
  • A slice is a part of a text or a list: text[2:5] goes from index 2 up to index 5, without index 5. text[:3] is the first three characters, text[2:] everything from index 2 on, text[-3:] the last three. scores[:3] is the first three elements of a list. A slice never ends with an IndexError.
  • Decimal places: f"{price:.2f}" prints a number with two decimal places, f"{average:.1f}" with one. It rounds only what is printed; the variable keeps its value.

Now put it all together. Players have signed up for a tournament by typing their names into one text, separated by commas, each the way they liked.

In the next lesson, Tuples and Sets, you'll meet two more ways of keeping several values together: tuples, for values that belong together and don't change, and sets, which keep each value only once.

Instructions

  1. 1

    Make a list players of the names, each without the spaces around it and with only its first letter in uppercase. Then print the players on one line, separated by a comma and a space: Players: Anna, Tom, Eva, Max. Keep the line with signups: the exercise also tries other sign-ups in it.

  2. 2

    The first three players to sign up get a T-shirt. Take them from players with a slice and print them the same way: T-shirts: Anna, Tom, Eva.

Back

12/12

  1. 1. Upper and Lower Case
  2. 2. Trimming Spaces
  3. 3. Replacing Text
  4. 4. Quiz: Text Methods
  5. 5. Searching in Text
  6. 6. Beginnings and Ends
  7. 7. Splitting Text
  8. 8. Joining a List
  9. 9. Slices
  10. 10. Quiz: Slices
  11. 11. Decimal Places
  12. 12. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.