首页 > 编程语言 > 详细

Python学习笔记字符串操作之startswith()和endswith()方法,检查开始和结束位置

时间:2018-10-08 20:04:10      阅读:160      评论:0      收藏:0      [点我收藏+]

 随笔记录方便自己和同路人查阅。

#------------------------------------------------我是可耻的分割线-------------------------------------------

  startswith()和endswith()方法返回True,如果它们所调用的字符串以该方法传入的字符串开始或结束。

否则,方法返回False。

#------------------------------------------------我是可耻的分割线-------------------------------------------

  1、startswith()方法,示例代码:

#
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
#被调用字符串开始位置是,startswith()方法传入的值
startswith_name = ‘hello world!‘
if startswith_name.startswith(‘hello‘):
    print(‘True‘)
else:
    print(‘False‘)

#被调用字符串开始位置不是,startswith()方法传入的值
startswith_name = ‘hello world!‘
if startswith_name.startswith(‘world‘):
    print(‘True‘)
else:
    print(‘False‘)

  运行结果:

技术分享图片

  2、endswith()方法,示例代码:

#
# -*- coding:utf-8 -*-
# # Autor: Li Rong Yang
#被调用字符串结束位置是,endswith()方法传入的值
startswith_name = ‘hello world!‘
if startswith_name.endswith(‘world!‘):
    print(‘True‘)
else:
    print(‘False‘)

#被调用字符串结束位置不是,endswith()方法传入的值
startswith_name = ‘hello world!‘
if startswith_name.endswith(‘hello‘):
    print(‘True‘)
else:
    print(‘False‘)

  运行结果:

技术分享图片

 

Python学习笔记字符串操作之startswith()和endswith()方法,检查开始和结束位置

原文:https://www.cnblogs.com/lirongyang/p/9568289.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!