String Boolean Tests
January 4, 2018
String Boolean Tests
Control structures form a large part of any workflow, and having a good understanding of the boolean checks that we can perform on strings is essential for data wrangling prior to analysis.
Case Checks
isupper()
returns True
if all characters in the string are uppercase.
'HELLO'.isupper()
True
'Hello'.isupper()
False
islower()
returns True
if all characters in the string are lowercase.
'hello'.islower()
True
'Hello'.islower()
False
istitle()
returns True
if all starting characters in the string are capitalised.
'Hello World'.istitle()
True
'hello world'.istitle()
False
Start and End Checks
Do note that Python is a case-sensitive language, as you can see from the following examples.
'Hello'.startswith('H')
True
'Hello'.startswith('h')
False
'Hello'.endswith('o')
True
'Hello'.endswith('O')
False
Type Checks
isalnum()
checks if all characters in the string are alphanumeric.
'abc123'.isalnum()
True
'abc123!'.isalnum()
False
isalpha()
checks if all characters in the string are alphabets.
'abc123'.isalpha()
False
'abc'.isalpha()
True
isdigit()
checks if all characters in the string are numbers.
'123'.isdigit()
True
'123a'.isdigit()
False
isspace()
checks if all characters in the string are spaces.
' '.isspace()
True
' a '.isspace()
False