This question already has answers here:
Check if string is upper, lower, or mixed case in Python
(2 answers)
Closed 8 years ago.
Is there a property of characters that returns the case? Like maybe char.case()? I need this to get rid of some repetitive code that's bothering me.
The functions isupper() and islower () are what you need
>>> 'a'.isupper()
False
>>> 'A'.isupper()
True
>>> 'b'.islower()
True
>>> 'B'.islower()
False
You can use str.isupper() and str.islower()
>>> 'a'.isupper()
False
>>> 'a'.islower()
True
>>> 'A'.isupper()
True
>>> 'A'.islower()
False
Related
This question already has answers here:
Why is True returned when checking if an empty string is in another?
(5 answers)
Closed 3 years ago.
'''
Here it goes (in interpreter):
a=''
a in '12345'
True
'''
The empty string is in all strings, that's why it evaluates to True.
>>> a=' '
>>> a in '12345'
>>> False
vs
>>> a=''
>>> a in '12345'
>>> True
You always get an empty string from any string. Reason:
>>>a=''
>>>b='12345'
>>>a in b
True
>>>b[0:0] # == a
''
You can read more in here.
This question already has answers here:
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 4 years ago.
I am a bit confused with if/else statement. Why the code always prints True while it should be False.
I have tried with different variables like i =10, i = 'a', i = 25. And it will be False if i=[]
This is my code:
i =1
if i:
print True
else:
print False
In your code you say if I: True. But your not comparing it to anything. You need a comparison operator. Like if i == 1 otherwise the if statement will just be true IF I has a value by default
This question already has answers here:
What's the canonical way to check for type in Python?
(15 answers)
Closed 5 years ago.
Why does this print False in Python 3?
>>> 1 == int
False
Because that isn't at all doing what you think it is. You are comparing the integer value 1 with the type int; naturally they are not equal.
If you want to check whether an object is of a certain type, use isinstance:
isinstance(1, int)
I guess, what you want to use is this:
>>> type(1) is int
True
or
>>> type(1) == int
True
This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Closed 8 years ago.
I tried this operation in python interactive mode :
>>> (1*1) is 1
True
>>> (377*35) is 13195
False
>>> 377*35
13195
>>> 377*35 is 377*35
False
>>> 1*1 is 1
True
Could anybody explain why ' (377*35) is 13195 ' is false?
Thanks in advance!
A is B checks that A and B refer to the same object. It does not check whether A equals B numerically.
The reason for the different behaviour in your examples is that ints with small values (typically between -1 and 99
inclusive) are "interned" by the interpreter -- whenever a result has such a value,
an existing short int with the same value is returned.
This explains why is returns True for your examples involving small numbers but not for those involving large numbers.
This question already has answers here:
Adding the number 1 to a set has no effect
(5 answers)
Closed 9 years ago.
Condider the following example:
>>> {1, True}
set([True])
>>
>>> {True, 1}
set([1])
Why is the set represented differently, depending on the order of the elements?
This happens because 1 and True are equal to each other:
>>> True == 1
True
>>> 1 == True
True
The set retains one element from each equality class.
bool is subclass of int class
>>> issubclass(bool, int)
True
>>> True+1
2
>>> True == 1
True