Start learning free

LESSON 78 OF 287 · STRINGS - CHALLENGES

File Extension in Python: split() and Negative Index

Practice Read the full explanation

Given a file name fileName, return its extension: the part of the name after the last dot.

Split the name at every dot, and the extension is the last of the parts. A name can have more than one dot, like "archive.tar.gz": only what comes after the last dot is the extension. Keep its letters exactly as they are.

 

Example 1:

Input: fileName = "photo.jpeg"
Output: "jpeg"
Explanation: The name has one dot, and "jpeg" comes after it.

Example 2:

Input: fileName = "archive.tar.gz"
Output: "gz"
Explanation: Splitting at the dots gives "archive", "tar" and "gz". The last part, "gz", is the extension.

Example 3:

Input: fileName = "README.MD"
Output: "MD"
Explanation: The extension keeps its upper-case letters.

 

Constraints:

  • fileName has at least one dot, and doesn't start or end with a dot.
  • fileName has no spaces: only English letters, digits, dots, - and _.
STEP 1 OF 2
    Ctrl + Enter to check

    PYTHON · STRINGS - CHALLENGES

    File Extension in Python, explained

    Problem overview

    The method fileExtension receives a file name, fileName, and returns its extension: the part after the last dot. A name can contain several dots, but only what follows the final one is the extension, and its letters are kept exactly as written. The name has at least one dot, doesn't start or end with a dot, and has no spaces.

    For "photo.jpeg" the result is "jpeg". For "archive.tar.gz" it is "gz", not "tar.gz". For "README.MD" it is "MD", with the upper-case letters kept.

    Approach

    Cut the name at every dot, then take the last piece. The string method split does the cutting: it returns a list of the parts between the separators. Taking the last item of that list is then a matter of indexing, and a negative index counts from the end of a list just as it does for a string. The approach doesn't depend on how many dots there are, because the last part is always the one after the final dot.

    Step by step

    1. Split the name at the dots. fileName.split(".") returns a list, which the solution stores in parts. For "archive.tar.gz" the list is ['archive', 'tar', 'gz'], and for "photo.jpeg" it is ['photo', 'jpeg'].
    2. Return the last item. parts[-1] is the last item whatever the length of the list: 'gz' for the first list, 'jpeg' for the second.

    The solution in Python

    class Solution:
        def fileExtension(self, fileName: str) -> str:
            parts = fileName.split(".")
            return parts[-1]
    

    Some details worth knowing:

    • split(".") takes the separator as a string, and every occurrence is a cut. The separator itself doesn't appear in the parts.
    • The result is a list of strings. Indexing a list with -1 works the same way as indexing a string with -1.
    • Nothing is converted: "README.MD".split(".")[-1] is "MD". If you wanted a lower-case extension, you would call lower() on it, but this lesson asks for the letters as they are.
    • The parts are new strings. fileName itself is not changed.

    Other ways to get the same result

    Python has string methods made for cutting at the last separator. "archive.tar.gz".rsplit(".", 1)[-1] and "archive.tar.gz".rpartition(".")[2] both give "gz". The lesson's two-step version with split is the clearest to read, and it works well here because file names are short. A name with a single dot is the simplest case: split gives two parts and the extension is the second.

    Edge cases and common mistakes

    • Several dots. Take the last part, not the second one. For "archive.tar.gz", parts[1] would be "tar", which is wrong.
    • The first part by mistake. parts[0] is the base name, "archive".
    • Splitting on the wrong separator. split(",") or split(" ") finds nothing to cut and gives a list with the whole name as its only item.
    • Names the constraints rule out. A name ending with a dot, like "a.b.", gives ['a', 'b', ''], so the last item is an empty string. A name with no dot, like "noext", gives ['noext'], and the code returns the whole name. The lesson doesn't pass either kind.
    • Changing case. Don't lower-case the extension. "MD" must stay "MD".

    Testing

    • "photo.jpeg" gives "jpeg".
    • "archive.tar.gz" gives "gz".
    • "README.MD" gives "MD".
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free