关于字符串的内置函数swith的编程练习

来源:1-1 本周学习安排

慕UI5149808

2021-04-30 18:04:30

http://img.mukewang.com/climg/608bd3ea0963dd6105480466.jpg老师你好,这节课我觉得我学懂了,但是这个编程练习我有些不理解的地方,根据题目我做出了以下的结果

#  coding:utf-8

str_1 = "My name is xiaomu, I'm from BeiJing."
start = str_1.startswith("xiaomu",len("My name is "))
end = str_1.endswith("Beijing",len("My name is xiaomu, I'm from BeiJing"))

我不懂为什么 

start = str_1.startswith("xiaomu",len("My name is "))

返回的是True 请讲解一下

还有

end = str_1.endswith("Beijing",len("My name is xiaomu, I'm from BeiJing"))

这个返回的为什么是False,我真的搞不懂了,请帮我讲解一下谢谢

写回答

1回答

时间,

2021-04-30

同学,你好!

1、startswith() 方法用于检查字符串是否是以指定子字符串开头,第一个参数是检测的字符串,第二个参数是字符串检测的起始位置

​len("My name is ")得到的值是11,即从11的位置开始检测,str_1的11位置正好是从x开始,因此结果为True

http://img.mukewang.com/climg/608bda2d09df6dc907890278.jpg

2、endswith() 方法用于判断字符串是否以指定后缀结尾,第一个参数是要判断的字符串,第二个参数是字符串中的开始位置

len("My name is xiaomu, I'm from BeiJing")得到的值是35,即从35开始,而35之后没有内容了,因此结果为False

祝学习愉快!

0

0 学习 · 2433 问题

查看课程