【Python】Python判断字符串是否为空
warning:
这篇文章距离上次修改已过202天,其中的内容可能已经有所变动。
在Python中,可以使用if
语句和not
关键字来判断一个字符串是否为空。空字符串包括空格在内,都会被认为是空。
def is_empty(s):
return not s
# 示例使用
str1 = ""
str2 = " "
str3 = "Hello"
print(is_empty(str1)) # 输出: True
print(is_empty(str2)) # 输出: False
print(is_empty(str3)) # 输出: False
在这个例子中,is_empty
函数通过not s
检查传入的字符串s
是否为空或者False
。如果字符串不为空且至少包含一个字符,not s
会返回False
。
评论已关闭