关于字符串的内置函数swith的编程练习
来源:1-1 本周学习安排
慕UI5149808
2021-04-30 18:04:30
老师你好,这节课我觉得我学懂了,但是这个编程练习我有些不理解的地方,根据题目我做出了以下的结果
# 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回答
同学,你好!
1、startswith() 方法用于检查字符串是否是以指定子字符串开头,第一个参数是检测的字符串,第二个参数是字符串检测的起始位置
len("My name is ")得到的值是11,即从11的位置开始检测,str_1的11位置正好是从x开始,因此结果为True
2、endswith() 方法用于判断字符串是否以指定后缀结尾,第一个参数是要判断的字符串,第二个参数是字符串中的开始位置
len("My name is xiaomu, I'm from BeiJing")得到的值是35,即从35开始,而35之后没有内容了,因此结果为False
祝学习愉快!
相似问题