如何运用PYTHON字符串startswith和endswith

 时间:2026-02-16 01:45:41

1、打开JUPYTER NOTEBOOK,新建一个PY文档。

如何运用PYTHON字符串startswith和endswith

2、"率讨You are the king.".startswith("You")

运用startswith,我们可以判断字符串句子里面第一个单词是否为指定单词。

如何运用PYTHON字符串startswith和endswith

3、"You are the king.".startswith("you")

但是注意大写小写,因为大写和小写会被判定为不一样。

如何运用PYTHON字符串startswith和endswith

4、"You are the king.".startswith("Y")

除了单词以后,还可以判断字母。

如何运用PYTHON字符串startswith和endswith

5、"123321 is number".startswith("123")

除了字母以外,字符串里的数字也是可以判断的。

如何运用PYTHON字符串startswith和endswith

6、"123321 is number".startswith(123)

但是进行判断的时候要注意书写格式。

如何运用PYTHON字符串startswith和endswith

7、"You are the king.".endswith("king.")

除了判断前面,endswith则是判断后面的部分。

如何运用PYTHON字符串startswith和endswith

8、"You are the king.".endswith(" king."倘级舍)

如果指定有带空格,那么净追空格也是可以判断进去的。

如何运用PYTHON字符串startswith和endswith

9、"You are the king.".endswith("king")

但是要非常小心一个句子里面是否有句号逗号等。

如何运用PYTHON字符串startswith和endswith

10、"You are the king.".startswith("Y", 0)

"You are the king.".startswith("Y", 1)

"You are the king.".startswith("You", 0)

为了方便,我们还可以设置判断第几个字母或者单词为指定的数据。

如何运用PYTHON字符串startswith和endswith

11、"You are the king.".startswith("are", 0, 10)

"You are the king.".startswith("are", 4, 10)

也可以用范围来进行判断,但是起点一定要是一样的。

如何运用PYTHON字符串startswith和endswith

  • 第五人格威廉艾利斯天赋怎么加点
  • 清炒辣椒丝瓜的做法
  • 潍柴英致737汽车仪表数据怎么看
  • 如何在家做出美味的青萝卜葱花煎饼
  • 自己开车撞树上保险怎么赔?
  • 热门搜索
    浙江嘉兴旅游 海南旅游线路 圣地亚哥旅游攻略 株洲旅游景点大全 重庆旅游大巴租赁 文昌旅游攻略 惠州罗浮山旅游攻略 郑州市旅游 图瓦卢旅游 山东潍坊旅游景点