Python 如何检查str(变量)是否为空?

Python 如何检查str(变量)是否为空?,python,string,conditional-statements,Python,String,Conditional Statements,如何制作一个: if str(variable) == [contains text]: 状况 (或者别的什么,因为我很确定我刚才写的完全错了) 我正在尝试检查是否是随机的。从我的列表中选择是[“”,](空白)还是包含[“text”,]检查字符串是否为空的“Pythonic”方法是: import random variable = random.choice(l) if variable: # got a non-empty string else: # got an emp

如何制作一个:

if str(variable) == [contains text]:
状况

(或者别的什么,因为我很确定我刚才写的完全错了)

我正在尝试检查
是否是随机的。从我的列表中选择
[“”,]
(空白)还是包含
[“text”,]

检查字符串是否为空的“Pythonic”方法是:

import random
variable = random.choice(l)
if variable:
    # got a non-empty string
else:
    # got an empty string
如果str(变量)==[包含文本]:
条件,如何生成:

也许最直接的方法是:

if str(variable) != '':
  # ...

请注意,
如果不是…
解决方案测试的是相反的条件。

只要说
如果是s
如果不是s
。如

s = ''
if not s:
    print 'not', s
在你的例子中,如果我理解正确

>>> import random
>>> l = ['', 'foo', '', 'bar']
>>> def default_str(l):
...     s = random.choice(l)
...     if not s:
...         print 'default'
...     else:
...         print s
... 
>>> default_str(l)
default
>>> default_str(l)
default
>>> default_str(l)
bar
>>> default_str(l)
default

默认情况下,空字符串为False:

>>> if not "":
...     print("empty")
...
empty

您可以将字符串与空字符串进行比较:

if variable != "":
    etc.
但您可以将其缩写如下:

if variable:
    etc.

说明:
if
实际上是通过计算所给逻辑表达式的值来工作的:
True
False
。如果只是使用变量名(或“hello”之类的文字字符串)而不是逻辑测试,那么规则是:空字符串计为False,所有其他字符串计为True。空列表和数字0也算作false,大多数其他内容算作true。

如果变量包含文本,则:

len(变量)!=0

当然不是


len(变量)==0

对于python 3,您可以使用


有时我们在引号之间有更多的空格,然后使用这种方法

a = "   "
>>> bool(a)
True
>>> bool(a.strip())
False

if not a.strip():
    print("String is empty")
else:
    print("String is not empty")
在if-else中使用“

x = input()

if not x:
   print("Value is not entered")
else:
   print("Value is entered")

Python字符串是不可变的,因此在讨论其操作时具有更复杂的处理。请注意,带空格的字符串实际上是空字符串,但大小不为零。 让我们看看两种不同的检查字符串是否为空的方法: 方法#1:使用Len() 使用Len()是检查零长度字符串的最通用方法。尽管它忽略了一个事实,即只有空格的字符串实际上也应该被视为空字符串,即使它不是零

方法#2:使用not

Not运算符也可以执行类似于Len()的任务,并检查长度为0的字符串,但与上面相同,它认为只有空格的字符串也是非空的,这实际上不应该是真的


祝你好运

你的意思是说如果str(variable)=“我的文本”:
?你似乎有点困惑,我建议你读一本。可能是重复的我不明白。所以,如果我做“variable=random.choice(list)”,并且变量是空的“”,那么我就可以做条件“if variable:”,并且。。。是的,我真的不知道你写了什么…@user1275670,听起来你确实明白了。但为了以防万一,我又加了一个例子。简而言之,
的计算结果为
False
,所以如果你想在
s
为空字符串时得到
True
,你可以说
if not s
。呵呵,我有点理解if,True和False的原理。魔兽争霸3触发器编辑器:“如果(这个单位等于那个单位)==True”,我猜你也可以在魔兽争霸编辑器中省略
==True
部分。是的。从来没有想过,呵呵,但这绝对是有帮助的。这是不一样的。不要这样做。在[14]:a=“”[15]:如果a.replace(“”,):…:print 1…:在[16]:如果a.replace(“”,)==“”:…:print 1…:1Oh-right,@AndreaBergonzo说:当你把一个字符串与“”进行比较时,你在检查它是否为空——这算是假的。这本来是完美的,但是
bool(0)
返回
False
@kaushalagrawal
bool(0)
返回
False
是我所期望的。为什么不应该?是的,我不记得/不理解为什么我觉得在StackOverflow中评论发布的代码是一个很好的做法。虽然这个答案可能是正确的和有用的,但如果你在解释它如何帮助解决问题的同时,还包括一些解释,那就更好了。这在将来变得特别有用,如果有一个变化(可能无关)导致它停止工作,用户需要了解它曾经是如何工作的。对不起,我的英语很差,但在将来我会记住评论我的帖子。
{
test_str1 = "" 
test_str2 = "  "
  
# checking if string is empty 
print ("The zero length string without spaces is empty ? : ", end = "") 
if(len(test_str1) == 0): 
    print ("Yes") 
else : 
    print ("No") 
  
# prints No  
print ("The zero length string with just spaces is empty ? : ", end = "") 
if(len(test_str2) == 0): 
    print ("Yes") 
else : 
    print ("No") 
}
a = "   "
>>> bool(a)
True
>>> bool(a.strip())
False

if not a.strip():
    print("String is empty")
else:
    print("String is not empty")
x = input()

if not x:
   print("Value is not entered")
else:
   print("Value is entered")
{
test_str1 = "" 
test_str2 = "  "
  
# checking if string is empty 
print ("The zero length string without spaces is empty ? : ", end = "") 
if(len(test_str1) == 0): 
    print ("Yes") 
else : 
    print ("No") 
  
# prints No  
print ("The zero length string with just spaces is empty ? : ", end = "") 
if(len(test_str2) == 0): 
    print ("Yes") 
else : 
    print ("No") 
}