python字符串内建函数-startwith、endwith

python字符串内建函数-startwith、endwith

9、startwith

函数功能:该函数用于检验字符串是否是以指定字符串开头。

返回值:该函数的返回值为true或者false。当字符串是否是以指定字符串开头则返回true,否则饭后false。

函数语法str.startwith(prefix[,start[,end]])

  • prefix:检测的字符串
  • start:可选参数,用于设置字符串检验的起始位置
  • start:可选参数,用于设置字符串检验的结束位置
# startwith
str1 = "hello world I love python"
new_str = str1.startswith("hello")
print(new_str) # True

new_str = str1.startswith("hello",3)
print(new_str) # False

new_str = str1.startswith("hello",0,3)
print(new_str) # False

10、endwith

函数功能:该函数用于检验字符串是否是以指定字符串结尾。

返回值:该函数的返回值为true或者false。当字符串是否是以指定字符串结尾则返回true,否则饭后false。

函数语法str.endwith(prefix[,start[,end]])

  • prefix:检测的字符串
  • start:可选参数,用于设置字符串检验的起始位置
  • start:可选参数,用于设置字符串检验的结束位置
# endwith
str1 = "hello world I love python"
new_str = str1.endswith("python")
print(new_str) # True

new_str = str1.endswith("python",-3)
print(new_str) # False

new_str = str1.endswith("hello",3,-3)
print(new_str) # False

注:python字符串内建函数还有很多,查看更多点击下面链接:

  • python 字符串内建函数

  • python 字符串内建函数-find、index、count

  • python字符串内建函数-replace、split

  • python字符串内建函数-capitalize、title、upper

  • python字符串内建函数-ljust、rjust、center

  • python字符串内建函数-lstrip 、rstrip、strip

你可能感兴趣的:(python,#,基础知识)