#πŸ”’ Or

46 messages Β· Page 1 of 1 (latest)

cinder oar
#

how would i write
if name == "ben'" how can i nake it so its ben or Ben?

silver daggerBOT
#

@cinder oar

Python help channel opened

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.

grim totem
#

Use β€œOR” operator

cinder oar
#

whenever i add or in like this

#

if name == "ben" or "Ben" every thing i type in comes through true

terse sand
#

!d in

silver daggerBOT
#
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.

terse sand
#

!d str.casefold

silver daggerBOT
#

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)...
grim totem
#

name = input("Enter your name: ")

if name.lower() == "ben":
print("Hello, Ben!")

silver daggerBOT
#

Hey @grim totem!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
terse sand
#

!e ```py

Using or. Less than ideal.

name = 'Ben'
print(name == 'Ben' or name == 'ben')```

silver daggerBOT
terse sand
#

!e ```py

Using in. Slightly better.

name = 'Ben'
print(name in {'Ben', 'ben'})```

silver daggerBOT
cinder oar
#

so would using a .lower() function work instead of an or function

terse sand
cinder oar
terse sand
#

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.

cinder oar
#

so wait whats the difference

terse sand
#

If you want precisely that, use in.

#

!e py print('bEN'.casefold() == 'ben'.casefold())

silver daggerBOT
terse sand
#

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.

terse sand
#

Which is what you specified.

ebon mortar
#

should BEN be valid? What about bEN?

cinder oar
#

thx

#

but how can i get or commands working if i ever need to use them?

ebon mortar
#

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'

cinder oar
#

OHH thank you

ebon mortar
#

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"

silver daggerBOT
#
Python help channel closed for inactivity

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.