Hướng dẫn check empty python

How do I make a:

if str[variable] == [contains text]:

condition?

[or something, because I am pretty sure that what I just wrote is completely wrong]

I am sort of trying to check if a random.choice from my list is ["",] [blank] or contains ["text",].

Rik Poggi

27.3k6 gold badges63 silver badges81 bronze badges

asked Mar 29, 2012 at 13:34

3

You could just compare your string to the empty string:

if variable != "":
    etc.

But you can abbreviate that as follows:

if variable:
    etc.

Explanation: An if actually works by computing a value for the logical expression you give it: True or False. If you simply use a variable name [or a literal string like "hello"] instead of a logical test, the rule is: An empty string counts as False, all other strings count as True. Empty lists and the number zero also count as false, and most other things count as true.

answered Mar 29, 2012 at 13:46

alexisalexis

47k15 gold badges98 silver badges156 bronze badges

8

The "Pythonic" way to check if a string is empty is:

import random
variable = random.choice[l]
if variable:
    # got a non-empty string
else:
    # got an empty string

answered Mar 29, 2012 at 13:35

Daniel LubarovDaniel Lubarov

7,6941 gold badge36 silver badges55 bronze badges

Just say if s or if not s. As in

s = ''
if not s:
    print 'not', s

So in your specific example, if I understand it correctly...

>>> 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

answered Mar 29, 2012 at 13:36

senderlesenderle

139k35 gold badges206 silver badges231 bronze badges

2

Empty strings are False by default:

>>> if not "":
...     print["empty"]
...
empty

answered Mar 29, 2012 at 13:37

bricebrice

23.5k7 gold badges77 silver badges94 bronze badges

For python 3, you can use bool[]

>>> bool[None]
False
>>> bool[""]
False
>>> bool["a"]
True
>>> bool["ab"]
True
>>> bool["9"]
True

answered Apr 5, 2017 at 5:05

Thai TranThai Tran

9,6557 gold badges41 silver badges62 bronze badges

3

Some time we have more spaces in between quotes, then use this approach

a = "   "
>>> bool[a]
True
>>> bool[a.strip[]]
False

if not a.strip[]:
    print["String is empty"]
else:
    print["String is not empty"]

answered Aug 29, 2018 at 5:12

kamran kausarkamran kausar

3,6151 gold badge21 silver badges17 bronze badges

element = random.choice[myList]
if element:
    # element contains text
else:
    # element is empty ''

answered Mar 29, 2012 at 13:39

eumiroeumiro

198k33 gold badges294 silver badges259 bronze badges

How do i make an: if str[variable] == [contains text]: condition?

Perhaps the most direct way is:

if str[variable] != '':
  # ...

Note that the if not ... solutions test the opposite condition.

answered Mar 29, 2012 at 13:35

NPENPE

471k103 gold badges921 silver badges997 bronze badges

if the variable contains text then:

len[variable] != 0

of it does not

len[variable] == 0

answered Jan 8, 2016 at 22:34

CESCOCESCO

6,6167 gold badges48 silver badges79 bronze badges

use "not" in if-else

x = input[]

if not x:
   print["Value is not entered"]
else:
   print["Value is entered"]

answered Mar 6, 2020 at 10:51

{
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"] 
}

answered Jan 24, 2021 at 9:52

string = "TEST"
try:
  if str[string]:
     print "good string"
except NameError:
     print "bad string"

answered May 9, 2018 at 11:03

3

Python strings are immutable and hence have more complex handling when talking about its operations. Note that a string with spaces is actually an empty string but has a non-zero size. Let’s see two different methods of checking if string is empty or not: Method #1 : Using Len[] Using Len[] is the most generic method to check for zero-length string. Even though it ignores the fact that a string with just spaces also should be practically considered as an empty string even its non-zero.

Method #2 : Using not

Not operator can also perform the task similar to Len[], and checks for 0 length string, but same as the above, it considers the string with just spaces also to be non-empty, which should not practically be true.

Good Luck!

answered Jan 24, 2021 at 9:48

#empty variable
myvar = ""

#check if variable is empty
if not myvar:
    print["variable is empty"] # variable is empty

answered Jul 21 at 4:38

1

Chủ Đề