We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Join Strings

The + operator in Python can be used to concatenate (smoosh) strings together. For example:

print("hello" + " " + "world")
# hello world

author: str = "Tolkien"
message: str = "The " + "world never deserved " + author
print(message)
# The world never deserved Tolkien

Assignment

Complete the join_strings() function. It takes a list of strings and returns a new single string.

The new string is the concatenation of all the input strings from the list end-to-end, in order, with a comma between them. If the list is empty, return an empty string. For example:

string_list: list[str] = ["Annie", "Reiner", "Bertholdt"]
joined_string: str = join_strings(string_list)
print(joined_string)
# "Annie,Reiner,Bertholdt"

string_list = ["Eren", "Mikasa", "Armin"]
joined_string = join_strings(string_list)
print(joined_string)
# "Eren,Mikasa,Armin"

Do not use the built-in .join() method... we're trying to learn how this works manually.

Tips

  • The output string should not have a comma at the beginning or the end.
  • You can use negative indexes to slice strings (e.g. string[:-2]) just like you can with lists.