【Python】Python判断字符串是否为空
warning:
这篇文章距离上次修改已过440天,其中的内容可能已经有所变动。
在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。
评论已关闭