#π Or
46 messages Β· Page 1 of 1 (latest)
@cinder oar
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Use βORβ operator
whenever i add or in like this
if name == "ben" or "Ben" every thing i type in comes through true
!d in
6.10.2. Membership test operations
The operators in and not in test for membership. x in s evaluates to True if x is a member of s, and False otherwise. x not in s returns the negation of x in s. All built-in sequences and set types support this as well as dictionary, for which in tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression x in y is equivalent to any(x is e or x == e for e in y).
For the string and bytes types, x in y is True if and only if x is a substring of y. An equivalent test is y.find(x) != -1. Empty strings are always considered to be a substring of any other string, so "" in "abc" will return True.
!d str.casefold
str.casefold()```
Return a casefolded copy of the string. Casefolded strings may be used for caseless matching.
Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter `'Γ'` is equivalent to `"ss"`. Since it is already lowercase, [`lower()`](https://docs.python.org/3/library/stdtypes.html#str.lower) would do nothing to `'Γ'`; [`casefold()`](https://docs.python.org/3/library/stdtypes.html#str.casefold) converts it to `"ss"`.
The casefolding algorithm is [described in section 3.13 βDefault Case Foldingβ of the Unicode Standard](https://www.unicode.org/versions/Unicode15.1.0/ch03.pdf)...
name = input("Enter your name: ")
if name.lower() == "ben":
print("Hello, Ben!")
Hey @grim totem!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
!e ```py
Using or. Less than ideal.
name = 'Ben'
print(name == 'Ben' or name == 'ben')```
:white_check_mark: Your 3.12 eval job has completed with return code 0.
True
!e ```py
Using in. Slightly better.
name = 'Ben'
print(name in {'Ben', 'ben'})```
:white_check_mark: Your 3.12 eval job has completed with return code 0.
True
so would using a .lower() function work instead of an or function
I mentioned str.casefold for this reason.
sry i am new to python and did not know what this does
Many people use str.lower in the same or similar way they should be using str.casefold.
Apply it to both operands.
a.casefold() == b.casefold()
Or equivalent.
But this is further reaching than the question you initially asked.
so wait whats the difference
If you want precisely that, use in.
!e py print('bEN'.casefold() == 'ben'.casefold())
:white_check_mark: Your 3.12 eval job has completed with return code 0.
True
It starts to be important if you start using characters outside the standard English alphabet.
Otherwise str.lower will do.
But it's good to be aware of this all the same.
This gives you True if it's exactly one of the two.
Which is what you specified.
lower or casefold is always preferrable so you don't have to account for all cases
should BEN be valid? What about bEN?
the issue you were running into is that you need a full expression on either side of it
if name == 'ben' or name == 'Ben'
if name == 'ben' or 'Ben'
OHH thank you
this might make sense in english, but not in python
'Ben' is evaluated directly as a bool when paired with or
'Ben' evaluates to True
any non-empty string is considered "Truthy"
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.