下列字符串中,判断名字以xiaomu开头,地点以BeiJing结尾,如何编写代码,返回结果都为True呢?
str_1 = "My name is xiaomu, I'm from BeiJing."
根据以下任务提示,拓展编程思维,编写代码实现一下。
任务提示:
1、str.startswith(item, start, end),start和end是可选参数,指定了这两个值,表示在指定范围内检查字符串是否以指定内容开头
2、str.endswith(item, start, end),表示在指定范围内检查字符串是否以指定内容结尾
3、len()函数用于计算字符串的长度,判断名字以xiaomu开头,开始范围start可定义为len("My name is ")
4、判断地点以BeiJing结尾,end可定义为len("My name is xiaomu, I'm from BeiJing")
代码尝试:
#coding:utf-8
str_1 = "My name is xiaomu, I'm from BeiJing."
start = len('my name is ')
print(start)
end = len("My name is xiaomu, I'm from ")
print(end)
result = str_1.startswith('xiaomu',start)
print(result)
result1 = str_1.startswith('BeiJing',end)
print(result1) # 或者直接print(result,result1)