后缀,可以是单个字符,也可以是字符串,还可以是元组; start = 0, => 索引字符串的起始位置; end = len(str)) => 索引字符串的结束位置。返回值:布尔类型(True,Fa..._endswitch函数">
当前位置:   article > 正文

endswitch()函数_endswitch函数

endswitch函数
原文链接

一、功能:

判断字符串是否以指定字符或子字符串结尾。
  • 1

二、函数原型:

str.endswith("suffix",    	=> 后缀,可以是单个字符,也可以是字符串,还可以是元组;
	     	  start=0,  	=> 索引字符串的起始位置;
	      	  end=len(str))	=> 索引字符串的结束位置。
返回值:布尔类型(True,False
  • 1
  • 2
  • 3
  • 4

三、应用举例:

str = "i love python"
print("1:",str.endswith("n"))
print("2:",str.endswith("python"))
print("3:",str.endswith("n",0,6))# 索引 i love 是否以“n”结尾。
print("4:",str.endswith("")) #空字符
print("5:",str[0:6].endswith("n")) # 只索引 i love
print("6:",str[0:6].endswith("e"))
print("7:",str[0:6].endswith(""))
print("8:",str.endswith(("n","z")))#遍历元组的元素,存在即返回True,否者返回False
print("9:",str.endswith(("k","m")))

#元组案例
file = "python.txt"
if file.endswith("txt"):     
    print("该文件是文本文件")
elif file.endswith(("AVI","WMV","RM")):    
    print("该文件为视频文件")
else:    
    print("文件格式未知")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
'
运行
运行结果:
1: True
2: True
3: False
4: True
5: False
6: True
7: True
8: True
9: False

该文件是文本文件
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

原文链接

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号