Linked Questions

25 votes
2 answers
130k views

How to check if a string is null in python [duplicate]

I have a value cookie that is returned from a POST call using Python. I need to check whether the cookie value is empty or null. Thus I need a function or expression for the if condition. How can I do ...
Sandeep Krishnan's user avatar
3 votes
1 answer
566 views

python check string for emptieness - is this the elegant way [duplicate]

if var is not None and var !="" and var !=" ": # todo can I write it like this?: if var: # todo var is only String type.
doniyor's user avatar
  • 38k
-1 votes
1 answer
4k views

Why does 'if not ' check empty string in python [duplicate]

Hi I have a quick question regarding 'if not ..' in py. This is a function that should take a list of strings and return a string. I wondered what does the first line(if not strs) do? I guess it ...
JessZhu's user avatar
  • 15
0 votes
1 answer
2k views

Validation Rule - User input cannot be left blank - Python [duplicate]

I'm currently working on a program that has a validation rule for a name, which doesn't allow any punctuation to be entered. The input is stored in a variable, name, and then stored in a file later on....
Jamil's user avatar
  • 1
0 votes
1 answer
472 views

How do you detect specifically an integer input but also check to see if the input to something is blank? [duplicate]

I was wondering if it was possible to have an input variable look for an integer input but also be able to detect if the user leaves no input. I was wanting to make a simple app where the user chooses ...
mxrgan's user avatar
  • 11
0 votes
4 answers
312 views

Making a simple program, how to I make it so blank/spaces input doesn't count/add to it [duplicate]

I am making a simple program that creates a grocery list. Right now, I am having trouble with blank input being added to my list: when I hit enter with or without spaces, it adds the blank input as ...
Tye Chamberlain's user avatar
0 votes
2 answers
530 views

How to return default value in Python from no user input [duplicate]

I'm trying to have this return a default value of "stupid" if the user does not enter in their name, but I cannot get it to work. It just returns "Greetings " with no name. def ...
rclouser24's user avatar
1332 votes
15 answers
1.7m views

Why does comparing strings using either '==' or 'is' sometimes produce a different result? [duplicate]

Two string variables are set to the same value. s1 == s2 always returns True, but s1 is s2 sometimes returns False. If I open my Python interpreter and do the same is comparison, it succeeds: >>&...
jottos's user avatar
  • 20.4k
169 votes
20 answers
198k views

Efficient way to remove keys with empty strings from a dict

I have a dict and would like to remove all the keys for which there are empty value strings. metadata = {u'Composite:PreviewImage': u'(Binary data 101973 bytes)', u'EXIF:CFAPattern2': u''}...
ensnare's user avatar
  • 42.5k
120 votes
14 answers
618k views

How to check whether a str(variable) is empty or not?

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 ...
user1275670's user avatar
  • 1,315
7 votes
4 answers
30k views

Check string "None" or "not" in Python 2.7

Wondering if if not foo is None is the same as if foo? Using Python 2.7 and foo is a string.
Lin Ma's user avatar
  • 10.2k
6 votes
7 answers
14k views

Assigning empty value or string in Python

I would like to understand if there is a difference between assigning an empty value and an empty output, as follows: 1> Assigning a value like this string = "" 2> An empty value returned as output ...
Maddy's user avatar
  • 1,399
5 votes
5 answers
4k views

Python: Comparing empty string to False is False, why?

If not '' evaluates to True, why does '' == False evaluates to False? For example, the "voids" of the other types (e.g. 0, 0.0) will return True when compared to False: >>> 0 == False True &...
Tony Power's user avatar
  • 1,228
1 vote
8 answers
28k views

Count occurrences of a given character in a string using recursion

I have to make a function called countLetterString(char, str) where I need to use recursion to find the amount of times the given character appears in the string. My code so far looks like this. def ...
user123456789101112's user avatar
-1 votes
11 answers
5k views

Return empty string as True in given string

def first_and_last(message): if (message[0] == message[3]): return True elif (message[0] != message[3]): return False print(first_and_last("else")) print(...
vish's user avatar
  • 326

15 30 50 per page