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?

10
  • 2
    One is a space, the other is empry string. if myString == "": is VERY clear in the link you shared Commented Dec 30, 2022 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 ''. Commented Dec 30, 2022 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. Commented Dec 30, 2022 at 15:53
  • 1
    Does this answer your question? Commented Dec 30, 2022 at 15:56
  • 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. Commented Dec 30, 2022 at 16:28

1 Answer 1

0

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

"" is the empty string.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
Not just a non-empty string, '' in '' is also True.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.