Python习题册007:返回后缀名

任务007描述

用Python编写一个程序,接收用户输入的一个带后缀的文件名,返回其中的后缀名。

思路及示例

因为文件名与后缀是用圆点间隔的,可以利用字符串的split()方法将其拆分,并形成一个列表。因为后缀是在最后,可以用索引值将其取出打印。
列表的索引值有正向与反向两种,正向时第一个为0,以后依次为1、2、3……,反向时最后一个为-1,之前的依次为-2、-3…….

示例代表

fileName = input('Please input a file name with an extension:')
nameList = fileName.split('.')
print('The file extension is: ', nameList[-1])

运行效果:

Please input a file name with an extension:readme.txt
The file extension is:  txt

你可能感兴趣的:(Python习题册007:返回后缀名)