Introduction
In this lab, you will learn how to check if a string ends with a specific suffix in Python using the endswith() method. This skill is essential for tasks like file type identification and data validation.
The lab guides you through creating a Python script to check for .pdf and .txt suffixes. You'll learn how to use the endswith() method to determine if a string ends with a particular suffix and how to modify the script to check for different suffixes. Further steps will explore checking for multiple suffixes.
Learn About String Suffixes
In this step, you will learn about string suffixes and their importance in programming. A string suffix is a substring that occurs at the end of a string. Understanding suffixes is crucial for tasks like file type identification, data validation, and text processing.
Let's start by creating a simple Python script to illustrate string suffixes.
Open the VS Code editor in the LabEx environment.
Create a new file named
suffix_example.pyin the~/projectdirectory.~/project/suffix_example.pyAdd the following Python code to the
suffix_example.pyfile:filename = "document.pdf" if filename.endswith(".pdf"): print("The file is a PDF document.") else: print("The file is not a PDF document.")This code checks if the string variable
filenameends with the suffix.pdf. Theendswith()method is used for this purpose.Save the
suffix_example.pyfile.Run the script using the
pythoncommand in the terminal:python suffix_example.pyYou should see the following output:
The file is a PDF document.
Now, let's modify the script to check for a different suffix.
Open the
suffix_example.pyfile in the VS Code editor.Modify the code to check for the
.txtsuffix:filename = "document.txt" if filename.endswith(".txt"): print("The file is a text document.") else: print("The file is not a text document.")Save the
suffix_example.pyfile.Run the script again:
python suffix_example.pyYou should see the following output:
The file is a text document.
This demonstrates how to use the endswith() method to check for specific suffixes in strings. This is a fundamental technique for many programming tasks.
Use endswith() Method
In this step, you will delve deeper into the endswith() method and explore its various applications. The endswith() method is a powerful tool for checking if a string ends with a specific suffix. It returns True if the string ends with the specified suffix, and False otherwise.
Let's continue with the suffix_example.py file from the previous step. We will modify the script to make it more interactive.
Open the
suffix_example.pyfile in the VS Code editor.Modify the code to prompt the user for a filename and then check if it ends with
.txt:filename = input("Enter a filename: ") if filename.endswith(".txt"): print("The file is a text document.") else: print("The file is not a text document.")This code uses the
input()function to get a filename from the user. Then, it uses theendswith()method to check if the filename ends with.txt.Save the
suffix_example.pyfile.Run the script:
python suffix_example.pyThe script will prompt you to enter a filename.
Enter a filename:Enter
my_document.txtand press Enter. You should see the following output:The file is a text document.Run the script again and enter
my_document.pdf. You should see the following output:The file is not a text document.
Now, let's explore the case sensitivity of the endswith() method.
Open the
suffix_example.pyfile in the VS Code editor.Modify the code to check for
.TXT(uppercase):filename = input("Enter a filename: ") if filename.endswith(".TXT"): print("The file is a text document (uppercase).") else: print("The file is not a text document (uppercase).")Save the
suffix_example.pyfile.Run the script:
python suffix_example.pyEnter
my_document.txtand press Enter. You should see the following output:The file is not a text document (uppercase).
This demonstrates that the endswith() method is case-sensitive. To perform a case-insensitive check, you can convert the string to lowercase using the lower() method before using endswith().
filename = input("Enter a filename: ")
if filename.lower().endswith(".txt"):
print("The file is a text document (case-insensitive).")
else:
print("The file is not a text document (case-insensitive).")
This modified code will correctly identify my_document.txt as a text document, regardless of the case of the suffix.
Check Multiple Suffixes
In this step, you will learn how to check if a string ends with any one of multiple suffixes. This is useful when you want to identify files based on a list of possible extensions.
Let's modify the suffix_example.py file to check for multiple suffixes.
Open the
suffix_example.pyfile in the VS Code editor.Modify the code to check if the filename ends with either
.txtor.pdf:filename = input("Enter a filename: ") suffixes = (".txt", ".pdf") if filename.endswith(suffixes): print("The file is either a text document or a PDF document.") else: print("The file is neither a text document nor a PDF document.")In this code, we define a tuple called
suffixescontaining the suffixes we want to check for. Theendswith()method can accept a tuple of suffixes as an argument.Save the
suffix_example.pyfile.Run the script:
python suffix_example.pyThe script will prompt you to enter a filename.
Enter a filename:Enter
my_document.txtand press Enter. You should see the following output:The file is either a text document or a PDF document.Run the script again and enter
my_document.pdf. You should see the following output:The file is either a text document or a PDF document.Run the script again and enter
my_document.docx. You should see the following output:The file is neither a text document nor a PDF document.
Now, let's add another suffix to the tuple.
Open the
suffix_example.pyfile in the VS Code editor.Modify the code to check if the filename ends with
.txt,.pdf, or.docx:filename = input("Enter a filename: ") suffixes = (".txt", ".pdf", ".docx") if filename.endswith(suffixes): print("The file is either a text document, a PDF document, or a Word document.") else: print("The file is neither a text document, a PDF document, nor a Word document.")Save the
suffix_example.pyfile.Run the script:
python suffix_example.pyEnter
my_document.docxand press Enter. You should see the following output:The file is either a text document, a PDF document, or a Word document.
This demonstrates how to use the endswith() method with a tuple of suffixes to check for multiple possible file types.
Summary
In this lab, you learned about string suffixes and their importance in programming, particularly for tasks like file type identification. You created a Python script using the endswith() method to check if a filename string ends with specific suffixes like ".pdf" and ".txt".
The lab demonstrated how to modify the script to check for different suffixes and verified the output in each case, illustrating the fundamental technique of using endswith() for string suffix validation.



