LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

Python empty String

I have difficulty understanding this line:

`if not gender`:

I believe it is checking for an empty string as per the condition :

Use an if statement to check if the value is an empty string and, if so, give it a descriptive value.

So I read this as — if it is not true that the variable ‘gender’ = gender.title()

Is it looking at the content of — gender variable — ie ‘Male’ of ‘Female’ or only its type which I assume could only be string because of the title() method?

I am not entirely sure if it is saying:

  • if not String?
    or
    if not ‘Male’/‘Female’

I read that empty strings are false by default so I think it would evaluate to
if not false/empty string and thereby be true but I do not see where the " "
in : if not " "
would come from since gender was assigned like this - gender = row[5] and do not see empty strings in the dataset . Please advise.

for row in moma:
gender = row[5]

# convert the gender to title case
gender = gender.title()

# if there is no gender, set
# a descriptive value
if not gender:
    gender = "Gender Unknown/Other"
row[5] = gender

https://app.dataquest.io/m/351/cleaning-and-preparing-data-in-python/5/string-capitalization

it checks if the string is empty, that is if gender == "".

Hi there,

Thanks I think I understand, so since gender has been assigned to –

gender = gender.title()

and title() method returns a string it is saying if it is not title() and therefore not a string and that is an empty string.

Regards
JB