-1

I've been trying to use Python's one function dedicated to making every letter in a string uppercase, but after using it on a string booleans don't work on that string. What do I mean you ask? Well let's just say the following booleans:

"b".upper() is "B"
".".upper() is "."
".".upper() is ".".upper()

are all false.

I'm so confused. It acts like after I convert a string to upper() form, instead of returning the same string with uppercase letters (which is what the documentation says it does), it returns a whole new object that doesn't equal anything even an object produced in the exact same conditions.

Setting breakpoints and looking at the actual values of the variables seems fruitless. I hover over is_graduate_input = input("Enter (y/n)").upper() and it shows me a value of "Y", because that's what I entered. But then directly after that line my print(is_graduate_input is "Y") statement prints out False?!?! Why?!?!

The documentation for python says that their algorithm for making letters uppercase is described in some Unicode standard. I don't want to read through it. I don't think it will help. Can someone just please tell me what the hell is going on. I want to go to bed. I have school tomorrow.

  • See [here](http://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python) - it's a bad idea to use `is` for comparing with anything except singletons like `None`. Use `==`. Very similar to what happens in Java with `==` and `.equals()`. – yeputons Feb 16 '17 at 03:52
  • @yeputons Is that my problem?!?! What should I be doing? – Dean Valentine Feb 16 '17 at 03:53
  • It is, you should use `==` in Python for comparing objects for equality. Do not use `is` outside of `is None` and `is not None` comparisons, unless you clearly understand what you're doing and why. – yeputons Feb 16 '17 at 03:53
  • Did you try Googling Python string comparison? – TigerhawkT3 Feb 16 '17 at 03:58

1 Answers1

1

is will return True if two variables point to the same object id

== will return True if the objects referred to by the variables are equal.

What you are doing is comparing two different objects here and hence getting False.

"b".upper() is "B" # will return False

What will work for you is

"b".upper() == "B"  # will return True

Say you start with

a = "b"
b = "B"
print(id(a))
print(id(a)) # will print same id as above.
print(id(b))
print(id(b)) # will print same id as above.

output:

4337047792
4337047792
4337610568
4337610568

But if you print id for a.upper() it will return a new object each time and print a new id.

print(id(a.upper()))
print(id(a.upper()))

output:

4372772488
4372772376

This is the reason why "b".upper() is "B" # will return False. Because the id wont match.

you can read up more on these threads:

why does comparing strings in python using either or is sometimes produce

understanding pythons is operator

Community
  • 1
  • 1
Vikash Singh
  • 13,213
  • 8
  • 40
  • 70