Truth Value of a String in Python

Truth value of a string in python

In python, any string except an empty string defaults to True

ie,

if "MyString":
# this will print foo
print("foo")

if "":
# this will NOT print foo
print("foo")

Understanding the truthiness of strings

See the truth value testing and comparisons sections of the documentation, excerpted below.

In a nutshell, most things are truthy by default, which is why bool("dog") is true. The == operator compares two objects for equality, as opposed to comparing their truthinesses, as I assume you had expected.

4.1. Truth Value Testing

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below.

By default, an object is considered true unless its class defines
either a __bool__() method that returns False or a __len__() method
that returns zero, when called with the object.

Here are most of the built-in objects considered false:

  • constants defined to be false: None and False
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: '', (), [], {}, set(), range(0)

Operations and built-in functions that have a Boolean result always
return 0 or False for false and 1 or True for true, unless otherwise
stated. (Important exception: the Boolean operations or and and
always return one of their operands.)

4.3. Comparisons

Objects of different types, except different numeric types, never
compare equal.

...

Non-identical instances of a class normally compare as non-equal
unless the class defines the __eq__() method.

What's the logical value of string in Python?

Any non empty string in Python (and most other languages) is true as are all non-zero numbers and non-empty lists, dictionaries, sets and tuples.1

A nicer way to do what you want is:

name = input("what is your name?")
if name in ("Kamran", "Samaneh"):
print("That is a nice name")
else:
print("You have a boring name ;)")

This creates a tuple containing the names that you want and performs a membership test.

1 As delnan points out in the comments, this applies to all well written collections. That is, if you implement a custom collection class, make sure that it is false when it's empty.

Truth value of Series in ambiguous for strings in language detection problem

You can apply function only for rows matched by condition in DataFrame.loc for also select column name:

#!pip install googletrans
from googletrans import Translator
translator = Translator()

mask = df['text_language'] !='en'

df['Comments_translated'] = df['Comments']
f = lambda x: translator.translate(x, dest='en').text
df.loc[mask, 'Comments_translated'] = df.loc[mask, 'Comments'].apply(f)
print (df)
text_language Comments Comments_translated
0 en Hello World Hello World
1 de Hallo Welt Hello World
2 it Ciao mondo Hello World
3 unknown ciao mon hello mon
4 ru Привет мир Hello World

Getting ambiguous truth value for string in for loop and not sure why

You have a numpy array somewhere, not standard Python types; see ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() for an example with explanation as to why numpy arrays throw this exception.

Given a basic Python list object containing standard string values, and search_string being a string too, your code just works:

>>> search_string = 'foo'
>>> my_strings = ["Let's foo the bar", 'There is spam in my egg salad', 'You are barred from entering the foodchain!']
>>> [my_str for my_str in my_strings if search_string in my_str]
["Let's foo the bar", 'You are barred from entering the foodchain!']

Error: The truth value of a DataFrame is ambiguous when splitting strings into two columns if two conditions are met

Your conditions are perfectly OK, the problem is the if statement - it reads:

if boolean_array :
...

but if needs just one boolean value, not a whole array of booleans. In order to reduce a boolean array to just one value you can use e.g. any() or all() as the error message suggests - if all(boolean_array): etc.

What you really want to do is probably:

results_threshold_50_split_ownership[(only_first_token) & (first_token_contain_floor)]['first'].str.split('Floors|Floor', expand=True)

i.e. use the boolean array for boolean indexing.

Update as per comment below:
You can assign the result of the splitting to the orginal columns using results_threshold_50_split_ownership.loc[(only_first_token) & (first_token_contain_floor), ['first', 'second']]. In this can you need to make sure, however, that maximum 2 columns are being return by specifying n=1 in the split function (in case your fist column contains the word 'floor' multiple times).

Example:

results_threshold_50_split_ownership = pd.DataFrame({'first': ['first floor value', 'all floors values', 'x'],
'second': ['y', None, None]})
print(results_threshold_50_split_ownership)
# first second
#0 first floor value y
#1 all floors values None
#2 x None
only_first_token = pd.isna(results_threshold_50_split_ownership['second'])
first_token_contain_floor = results_threshold_50_split_ownership['first'].str.contains('floors|floor',case=False)
results_threshold_50_split_ownership.loc[(only_first_token) & (first_token_contain_floor), ['first', 'second']] = results_threshold_50_split_ownership[(only_first_token) & (first_token_contain_floor)]['first'].str.split('floors|floor', 1, expand=True).to_numpy()
print(results_threshold_50_split_ownership)
# first second
#0 first floor value y
#1 all values
#2 x None

Converting from a string to boolean in Python

Really, you just compare the string to whatever you expect to accept as representing true, so you can do this:

s == 'True'

Or to checks against a whole bunch of values:

s.lower() in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']

Be cautious when using the following:

>>> bool("foo")
True
>>> bool("")
False

Empty strings evaluate to False, but everything else evaluates to True. So this should not be used for any kind of parsing purposes.



Related Topics



Leave a reply



Submit