Python Forum
Boolean: if variable is capitalized
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Boolean: if variable is capitalized
#1
vehicle = "Alpa Romeo"
vehicle.iscapitalize()
I want to check if variable is capitalized ( if first letter is capital ) and this code doesn't work. For other True/False tests we use prefix "is", for example .isalnum() or .isalpha but here I receive AttributeError. On the other hand, for capitalizing variable capitalize() is used. This doesn't make sense to me. Please advise.
Reply
#2
The method you need is

vehicle.isupper()
Reply
#3
I think you need istitle()

>>> vehicle = "Alfa Romeo"
>>> vehicle.istitle()
True
>>> vehicle.capitalize()
'Alfa romeo'
do you see the difference between title and capitalize?
Reply
#4
Sorry. Buran is right. I misread you original post. You definitely need .istitle() method not .isupper(). Note .isupper() is used to check if all characters in a string is in upper case.

I'm more interested in the latter part of the statement. For other True/False tests we use prefix "is", for example .isalnum() or .isalpha but here I receive AttributeError. Can you elaborate? When do you get AttibuteError. Could you give us an example?
Reply
#5
I followed the wrong logic: .upper() - .isupper(). Instead of .iscapitalize(), .istitle() should be used. I receive AttributeError when using .iscapitalize()
Reply
#6
When in doubt, you can ask nicely what's available:
>>> dir("")
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
From there, you can see what one of those is:
>>> help("".isupper)
Help on built-in function isupper:

isupper(...) method of builtins.str instance
    S.isupper() -> bool

    Return True if all cased characters in S are uppercase and there is
    at least one cased character in S, False otherwise.

>>> help("".istitle)
Help on built-in function istitle:

istitle(...) method of builtins.str instance
    S.istitle() -> bool

    Return True if S is a titlecased string and there is at least one
    character in S, i.e. upper- and titlecase characters may only
    follow uncased characters and lowercase characters only cased ones.
    Return False otherwise.
Reply
#7
You can also write your own version
def iscapitalized(s):
    """iscapitalized(S) -> bool

    Returns True if S is a capitalized string and there is
    at least one character in S.
    """
    return bool(s) and (s.capitalize() == s)
Reply
#8
Hi Griboullis,

I don't quite understand your code, how does computer know that bool(s) is True? Why not false?
Reply
#9
Any non-empty string is True.

>>> bool("test")
True
>>> bool(" ")
True
>>> bool("")
False
Personally, I think that would look better without the explicit bool() call:
def is_caps(s):
    return s and (s.capitalize() == s)
Reply
#10
(Jan-09-2018, 10:40 PM)nilamo Wrote: Personally, I think that would look better without the explicit bool() call:
There is a tiny difference: your function returns '' instead of False when it is called with the empty string.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning a Boolean operator and a variable both? PythonAndArduino 2 2,919 Nov-08-2017, 08:31 PM
Last Post: PythonAndArduino

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020