-1

To my amateur knowledge, an empty string is a string that has length zero. So that must differ from a string that is just spaces right? Even though printing both will appear the same.

I read here (Most elegant way to check if the string is empty in Python?) how to recognize an empty string, but when running this bit of code on what I think is an empty string, it fails:

print("fish:", fish)

if not fish:       #if fish is empty string
    fish = weight
    print("new fish:", fish)
else:
    print("fish is not an empty string")

A printout of what fish is:

fish:  

Am I missing something very simple here? Or is fish indeed a string of spaces and that's why the empty string search fails?

Thanks.

theprowler
  • 3,138
  • 11
  • 28
  • 39
  • You could use ``print("fish: <{}>".format(fish))`` - then you would see ``fish: < >`` – Mike Scotty Jun 16 '17 at 18:12
  • It's really easy to accidentally mangle spaces when posting output like this, but from what we can see, it looks like your `fish` string has a single space in it. – user2357112 Jun 16 '17 at 18:12
  • i guess, you can probably use `fish.strip()` first. – Wasi Ahmad Jun 16 '17 at 18:12
  • 1
    Try `print(repr(fish))`. That'll add surrounding quotation marks. – Aran-Fey Jun 16 '17 at 18:12
  • Your code doesn't reproduce the problem. Most of all, you did not supply values for `fish` and `weight`. When I do so, `fish` finished with the value of `weight`, as expected. – Prune Jun 16 '17 at 18:14
  • 1
    It's possible that you have a non-printing character between the quotation marks. Try re-typing your assignment to `fish`. – Prune Jun 16 '17 at 18:15
  • If you want to check if something is an empty string: `my_string == ''` – juanpa.arrivillaga Jun 16 '17 at 18:19
  • @juanpa.arrivillaga: Why? [`if not my_string:` is perfectly valid/Pythonic](https://stackoverflow.com/a/9573259/364696), and faster to boot. While `str` is a special case (in Py3, there aren't many, if any, "`str`-like" things that aren't equality compatible), I'm sick of seeing code that does `if myseq == []:` when they really want to know if it's an empty sequence, because it means you're now tied to an equality compatible type, and can't just substitute, say, `tuple`s without (silently) misbehaving. – ShadowRanger Jun 16 '17 at 18:21
  • @ShadowRanger I generally agree. That was for debugging purposes. I would use the `if fish` construction in the code, likely. – juanpa.arrivillaga Jun 16 '17 at 18:24

1 Answers1

1

Try:

print("fish:", repr(fish))

which will quote the output and use escapes for non-printable characters so you can see what's really in there. If it's anything other than fish: '', it wasn't really empty. You could also just print len(fish), which would give you the length; the definition of "falsy" for sequences is "length of 0" after all.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • The output is `fish: ' ' ` – theprowler Jun 16 '17 at 18:13
  • @theprowler: Yeah, then you've got a single space character in there. If you want to ignore it, lead off with `fish = fish.rstrip()` to remove all trailing whitespace (which is usually not important to interpreting your value) or `fish.strip()` to remove both leading and trailing whitespace. – ShadowRanger Jun 16 '17 at 18:14
  • Ok gotcha. Is there a way to recognize if a variable is comprised of spaces? That way I can check if it's empty or if it's nothing but space? – theprowler Jun 16 '17 at 18:14
  • 1
    @theprowler: `if not fish.strip():` would be the trivial test if you don't want to keep the stripped version. `if not fish or fish.isspace():` works without making a temporary string, but it's multiple tests and kind of ugly; the single test of `if not fish.strip():` is likely to be faster unless the strings involved are enormous. – ShadowRanger Jun 16 '17 at 18:15
  • That solved it. So I was pretty dumb, it was as simple as use `strip()` to remove all spaces, if any, then proceed. Well thanks a ton – theprowler Jun 16 '17 at 18:17
  • 1
    @theprowler: To be clear, `strip` only removes leading and trailing whitespace. It won't remove internal whitespace. So `' spam eggs '.strip()` returns `'spam eggs'` (the space between the words is not removed). – ShadowRanger Jun 16 '17 at 18:18