How does python create an empty list?

Good point. So how does python denote an empty string? I mean other than the quotation marks the user sees. Does it have a value? Like none, undefined, True or false?

The only value an empty string can have is "" or ''. As we’ve seen already it is what the constructor returns. It’s not Python, as much as the str class definition for any string–a grouping of characters (or nothing) demarcated by quotes. There is no other associated value, and as @ionatan stated above, none is needed. It is nothing special.

I think that it’s a falsy value. There must be an associated Boolean value for testing, right?

>>> '' and False
''
>>> False and ''
False
>>> 

I’m not sure what this means. Does it mean that False and ‘’ are the same? Can you explain?

These results confuse me. Is an empty string Falsy, Truthy, or neither?

The image is rather long. Click it to see all of it.
image

Not the same, per se, but similar. False is False and '' is falsy.

AND short-circuits on False so the first operand is the yield of the logical expression,

False and ''    =>  False
'' and False    =>  ''

if follows the path of truthiness in the first branch, else it must be falsy. Consider,

>>> if False:
   print ('do this')
else:
   print ('Oops!')    # the path of falsiness

   
Oops!
>>> if '':
   print ('do this')
else:
   print ('Oops!')

   
Oops!
>>> not not ''        # `not` casts to boolean then negates
False
>>> not not False
False
>>> 

Think on that for awhile, and don’t leave the subject of AND, OR and NOT until you are sufficiently comfortable (and confident) with it. Make note of some of the axioms we’ve discussed, thus far.

I’m familiar and comfortable with AND, OR and NOT.

However, I’m not familiar with using Python on the command line. So I didn’t know that this was doing.

I do now.

But back to the original question. Does an empty string have a value? If it’s not False and it’s not True is the falsiness considered a value?

1 Like

The only value an empty string has is '' or "", or if we stretch… str(). That’s it. When evaluated in a boolean expression it is falsy, but that’s the evaluation, not the value.

I like using the Shell because it responds immediately, without print(). Does something work? Try it in the shell, and see what happens.


Something to keep in mind when working with direct expressions, like above.

A and B

will yield the first operand value if falsy (or False), otherwise it yields the second operand value, whatever it may be. It does not necessarily yield a bool.

>>> '' and 'A'
''
>>> 'A' and ''
''
>>> 'A' and 'B'
'B'
>>> 

Throw an if in front of that and control flow is handed down the appropriate branch. Bear in mind that 'A' and 'B' is truthy.

OR, on the other hand behaves similarly, but it short-circuits on truthy.

>>> '' or 'A'
'A'
>>> 'A' or ''
'A'
>>> 'A' or 'B'
'A'
>>> 'B' or 'A'
'B'
>>> 

That’s not quite on topic, but something to explore when you have some play time. The point here is operand values and truthiness, which is evaluated.

>>> def foo(x):
	return x

>>> foo('a' or 'b')
'a'
>>> foo('a' and '')
''
>>> foo('a' and 'b')
'b'
>>> def foo(x):
	return not not x

>>> foo('a' and 'b')
True
>>> foo('a' or 'b')
True
>>> foo('a' and '')
False
>>> 

Thank Roy!

This is good stuff.

Here is Quora page I found about None type and strings. There’s some interesting stuff about how they are constructed. https://www.quora.com/What-is-the-difference-between-Null-and-empty-string-in-python

image

Thanks!

1 Like

As you can see, their only connection is how they are evaluated. They both show up falsy.

>>> def foo():
	pass

>>> foo() and 'A'
>>> 

Nothing was echoed since there was nothing to return.

>>> print(foo() and 'A')
None
>>> print(foo() or '')

>>> 

Notice that even an empty string gets a newline character concatenated.

Something else to consider,

>>> type(foo() and 'A')
<class 'NoneType'>
>>> type(foo() or '')
<class 'str'>
>>> 

Addendum

Why foo(), you may ask? It follows from my own logic that nothing should not have a literal (though it does, as we know) and so I let Python handle it whenever I want it for whatever purposes, like now.

There is no return from foo(), hence, None when we go to print it, but nothing in the console if we don’t print. Not even a newline character since None cannot be concatenated. This was intentional, with expected results.

Thank you! I know this stuff doesn’t get me through the course but it’s definitely interesting.

1 Like

Glad you’re getting some enjoyment out of it. That was the idea.

>>> '' == foo()
False
>>> 0 == foo()
False
>>> 0 > 1 == foo()
False
>>> 

Here’s the kicker,

>>> foo() == foo()
True
>>> 

The only thing equal to None, is None.

Don’t be fooled into thinking I consider this work. Pure play, which I can get away with. It might not count for much in my domain, given my age; but, it can sure count in yours. And, if you get enjoyment from it, I get it in Spades. Coding, like maths and nature are a joy to behold.


Last on the subject,

False

is not falsy. It is literally, False.

Same goes for True not being truthy. It is literally, True.

Boolean expressions don’t generally yield a literal, but an outcome. We have to coerce them into yielding a literal, as in,

not not ''

which will yield,

False

Of particular note, which has not been obvious thus far, we cannot express the truthiness (nor falsiness) of a value without the use of a logical (or conditional) expression.

I’ll close with this…

>>> print('')

>>> print([''])
['']
>>> 

Why output is like this…
[‘m’, ‘’, ‘ppi’]
[‘a’, ‘le’]

and not like this…
[‘m’, ‘’, ‘’, ‘ppi’]
[‘a’, ‘’, ‘le’]

???

2 Likes

I am completely new to python, for this question why we are using len -1

word = 'mississippi '
split_word = word.split()
split_with_separator = word.split(' ')
print(split_word)
print(split_with_separator)

Initially, I thought both print function will give the same result but it didn’t. please explain why?

I did not use split here, but let me try :slight_smile:

However below is my code for the question:

def count_multi_char_x(word,x):

c = 0

char_count = 0

length = len(x)

counter = 0

while counter < len(word):

    counter += 1

    if x == word[c:len(x)+c]:

        print(word[c:len(x)+c])

        char_count += 1

        c += 1

    else:

        c +=1 

return char_count

Here is my take on this (or you could just string’s .count() method)

def count_multi_char_x(word, x):
  lst = [word[i:len(x) + i] for i in range(len(word)) if word[i:len(x) + i] == x]
  return len(lst)

The split() function returns a list, with a length. In that instance of counting the instances of a string within a string you can return the split - 1. Here is my code for this:

def count_multi_char_x(word, x):
count = word.split(x)
return len(count) - 1

mtf stands for mother effing smart