Python Program - Extract Extension From the File Name

here's a Python program that extracts the extension from a file name:

‮efer‬r to:theitroad.com
filename = input("Enter a file name: ")

# find the position of the last dot in the filename
dot_position = filename.rfind(".")

# extract the extension from the filename
extension = filename[dot_position+1:]

# print the extension
print("Extension of the file is:", extension)

In this program, we first use the input() function to get a file name from the user, and store it in the variable filename.

We then use the rfind() method to find the position of the last dot (.) in the filename. The rfind() method searches the string from right to left, and returns the position of the last occurrence of the specified substring. In this case, we are searching for the dot character, so dot_position will contain the position of the last dot in the filename.

We then use slicing to extract the extension from the filename. The filename[dot_position+1:] expression extracts all characters in the filename string starting from the position immediately after the last dot (i.e., the extension).

Finally, we use the print() function to print out the extension of the file.