Get startedGet started for free

Split lines or split the line?

You are about to leave work when a colleague asks you to use your string manipulation skills to help him. You need to read strings from a file in a way that if the file contains strings on different lines, they are stored as separate elements. He also wants you to break the strings into pieces if you see that they contain commas.

The text of the file has been already saved in the variable file. You can use print(file) to view the variable in the IPython Shell.

This exercise is part of the course

Regular Expressions in Python

View Course

Exercise instructions

  • Split the string file into many substrings at line boundaries.
  • Print out the resulting variable file_split.
  • Complete the for-loop to split the strings into many substrings using commas as a separator element.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Split string at line boundaries
file_split = ____

# Print file_split
print(____)

# Complete for-loop to split by commas
for ____ in ____:
    substring_split = substring.____
    print(substring_split)
Edit and Run Code