0

Oke, i've looked into How to check if the string is empty? but it didn't help me. Also chat GPT talks weird if you dig into this question, and the python manual didnt help me either.

Language = python 3.11.1

previous_char = " "
vowels = 'aeiou'
print(previous_char in vowels)

this code evaluates as 'false' and length 1

But if you remove the space between the quotation marks in previous_char

previous_char = ""
vowels = 'aeiou'
print(previous_char in vowels)

this code evaluates as 'true' and length 0

So basically if you ask: is 'nothing' in vowels.. its true?? I don't find this logical, but on the other hand, if it would evaluate to false, it would also be weird.

I started coding 2 weeks ago for fun, i'm 35 years old, so please don't burn me to hard if this is some kind of dumb question.

But i'm a bit stuck in understanding why this is the way it is?

frank
  • 1
  • 2
    One is a space, the other is empry string. `if myString == "":` is VERY clear in the link you shared – azro Dec 30 '22 at 15:49
  • Try `'aeiou'.count('')`, and you'll get 6. Python seems to treat the gaps between characters and outersides of the string as `''`. – Mechanic Pig Dec 30 '22 at 15:50
  • It's a more useful property when using "advanced" tools like regular expressions, when an empty string may represent a pattern that is matched zero times. – snakecharmerb Dec 30 '22 at 15:53
  • 1
    Does [this](https://stackoverflow.com/questions/27603885/why-is-true-returned-when-checking-if-an-empty-string-is-in-another) answer your question? – snakecharmerb Dec 30 '22 at 15:56
  • ah, that seems to be something.. i tried the len() method before, then it gives 5, but with your proposed count('') method it gives 6 indeed.. still weird to me, butt it explains the behavior of the boolean check. – frank Dec 30 '22 at 15:59
  • Other possible duplicates - [this](https://stackoverflow.com/q/5206466/5320906) and [this](https://stackoverflow.com/q/43729159/5320906) – snakecharmerb Dec 30 '22 at 16:09
  • @snakecharmerb thanks for the links! very useful. I spent already 2 hours on experimenting with this problem myself, but i guess that's what learning/understanding is. If you go to the answer to soon or easy you'll learn nothing ;-) – frank Dec 30 '22 at 16:21
  • If they answer your question we can mark it as a duplicate of them - would that be ok? The idea of Stack Overflow is to try to keep answers to questions in a single place as much as possible. – snakecharmerb Dec 30 '22 at 16:23
  • 1
    @snakecharmerb yes no problem with me, they answer the question in more details then I understand at the moment :-) I didn't find those threads because i'm unfamiliar with more complex programming terminology but i learned a lot the past 2 weeks. – frank Dec 30 '22 at 16:28
  • With time and study will come deeper understanding. – snakecharmerb Dec 30 '22 at 16:30

1 Answers1

0

" " is a string containing a single character (a space).

"" is the empty string.

ndc85430
  • 1,395
  • 3
  • 11
  • 17
  • thanks, for the reply, thats what i also found out. But i still don't get why the check if the 'empty string' is IN 'aeiou' is true? and the check if the 'space' is in 'aeiou' false? – frank Dec 30 '22 at 15:52
  • 1
    Well, the second case should be obvious - there's no space character in the string `"aeiou"`, so you get a `False`. The first case can be seen like this: any string can be written as another string that is the concatenation of itself and the empty string (in your case `"aeiou" + ""` is still the string `"aeiou". Therefore, the empty string can be said to be part of a non-empty string. – ndc85430 Dec 30 '22 at 15:56
  • 1
    Not just a non-empty string, `'' in ''` is also `True`. – Mark Ransom Dec 30 '22 at 15:59
  • To be clear on the above, you can put the empty string _anywhere_ and it doesn't change the original string: `"aei" + "" + "ou"` still gives you the string `"aeiou"`. Basically, the empty string is like the string equivalent of the number 0 - adding or subtracting 0 to any number leaves it unchanged. – ndc85430 Dec 30 '22 at 15:59