字符串结尾判断(python练习)

编写一个程序来检查一个字符串是否以另一个字符串结尾。

  • 定义函数ends_with(),有两个参数string1string2
  • 在函数内,如果string1string2结尾,则返回True,否则返回False

示例输入

dragon on

示例输出

True

本文可以使用endswith()函数来判断字符串是否以某一字符串结尾。若是,则返回True,反之则返回False。

def ends_with(string1, string2):
    # 此处写你的代码
    return string1.endswith(string2)

# 获取输入字符串
string1 = input()
string2 = input()
# 调用函数
print(ends_with(string1, string2))
  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 将字符串"abcd"转换成大写的Python代码: ```python string = "abcd" string_upper = string.upper() print(string_upper) ``` 2. 计算字符串"cd"在字符串"abcd"中出现的位置的Python代码: ```python string = "abcd" substring = "cd" index = string.find(substring) print(index) ``` 3. 字符串“a,b,c,d",用逗号分割字符串Python代码: ```python string = "a,b,c,d" string_split = string.split(",") print(string_split) ``` 4. 将字符串"Python is good"里的Python替换成python,并输出替换后的结果的Python代码: ```python string = "Python is good" string_replace = string.replace("Python", "python") print(string_replace) ``` 5. 从字符串"python数据类型练习.html"中获得.html前面的部分的Python代码: ```python string = "python数据类型练习.html" substring = string.split(".")[0] print(substring) ``` 6. 获得字符串"python数据类型练习.html"的长度的Python代码: ```python string = "python数据类型练习.html" length = len(string) print(length) ``` 7. 将字符串"this is a book"里的book替换成apple的Python代码: ```python string = "this is a book" string_replace = string.replace("book", "apple") print(string_replace) ``` 8. 用程序判断字符串"this is a book"是否以this开头的Python代码: ```python string = "this is a book" if string.startswith("this"): print("Yes") else: print("No") ``` 9. 用程序判断字符串"this is a book"是否以apple结尾Python代码: ```python string = "this is a book" if string.endswith("apple"): print("Yes") else: print("No") ``` 10. 将字符串"This IS a book"里的大写字符转成小写字符的Python代码: ```python string = "This IS a book" string_lower = string.lower() print(string_lower) ``` 11. 将字符串"this is a book"里的小写字符转成大写字符的Python代码: ```python string = "this is a book" string_upper = string.upper() print(string_upper) ``` 12. 字符串"this is a book"末尾有一个回车符,请将其删除的Python代码: ```python string = "this is a book\n" string_strip = string.rstrip() print(string_strip) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值