Exercise 23 

Write a program in Python language that asks the user to enter the name of a file and return the extension. Example if the user enters coursPython.pdf the program returns the message "The file extension is .pdf".

Solution

 # ask user to enter a file name with extension
myFile = input("Enter the file name with extension: ")

# convert the file name to list by choosing the '.' caracter as separator
list = myFile.split(".")

# find the extension of file name
ext = list[-1]
print ("The file extension is : " + ext)
Younes Derfoufi

Leave a Reply