human定义一个判断字符串的长度是否大于10的函数。使用Python
可以使用Python内置函数len()来获取字符串的长度,然后判断是否大于10,可以这样实现:
def is_longer_than_10(string):
if len(string) > 10:
return True
else:
return False
也可以简化为一行代码:
def is_longer_than_10(string):
return len(string) > 10
使用示例:
>>> is_longer_than_10('hello world')
True
>>> is_longer_than_10('hello')
False
>>> is_longer_than_10('')
False
原文地址: https://www.cveoy.top/t/topic/fcU4 著作权归作者所有。请勿转载和采集!