← All lessons

split and join

Sign in · Sign up

Lesson 4.5

split cuts a string into parts, join glues the parts back together:

text = "I learn python"
words = text.split()      # ['I', 'learn', 'python']
print("-".join(words))    # I-learn-python

Remember the order: split is called on the string, and join on the separator:

  • text.split() — split the text by spaces;
  • "-".join(words) — glue the list together with a hyphen.

The right order is easy to remember: first what you call it on, then what you glue.

The text and the separator separator are set by the check. Print, in order:

  1. the list of words — the way print shows it;
  2. the words glued together with separator.
Hint

.split() cuts the string by spaces and returns a list.

.split("-") cuts by another separator. "-".join(list) builds the string back together — the separator goes before the dot.