2018年1月8日 星期一

Python: Input a filename and print the extension of that

Python: Input a filename and print the extension of that

Python Basic: Exercise-7 with Solution

Write a Python program to accept a filename from the user and print the extension of that.
Sample filename: abc.java
Python str.rsplit(sep=None, maxsplit=-1) function:
The function returns a list of the words of a given string using a separator as the delimiter string.
  • If maxsplit is given, the list will have at most maxsplit+1 elements.
  • If maxsplit is not specified or -1, then there is no limit on the number of splits.
  • If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings.
  • The sep argument may consist of multiple characters.
  • Splitting an empty string with a specified separator returns [''].
Sample Solution:-
filename = input("Input the Filename: ")
f_extns = filename.split(".")
print ("The extension of the file is : " + repr(f_extns[-1]))

========= RESTART: F:/Python_APSC/py-ex-basic-7.py ============
Input the Filename: ex3-1.py
The extension of the file is : 'py'
>>> 



沒有留言:

張貼留言

WOKWI LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...