Clean an imported filename
Before you start this beginner track
Variables, strings, and basic lists. Build up to dictionary counting.
Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.
Understand it, then try it.
Imported text can contain unwanted whitespace around a useful value. strip returns a string with whitespace removed from both ends, while leaving spaces inside the name alone. lower returns a lowercase version. Strings are immutable, so each result is assigned to a variable before the cleaned filename is printed.
raw_name = " guide.MD \n"trimmed_name = raw_name.strip()filename = trimmed_name.lower()print(filename)Run each standalone script with Python 3.8+. File names and log entries are in-memory sample text; no files, packages, or input are required.
Track the values line by line. Predict the result before running this example in your own editor.