Page 267 - Computer Science Class 11 Without Functions
P. 267
I am Good, Sir,
How are you
To remove the whitespace only from the beginning or end of the string, we use the Python string methods lstrip()
and rstrip(), as illustrated below:
>>> ' Hello How are you! '.lstrip()
'Hello How are you! '
>>> ' Hello How are you! '.rstrip()
' Hello How are you!'
s.isupper(): Tests whether the string comprises only uppercase characters.
s.islower(): Tests whether the string comprises only lowercase characters.
s.isalpha(): Tests whether a string comprises only alphabets.
s.isdigit(): Tests whether a string comprises only digits.
s.isalnum(): Tests whether a string contains only alphabets and digits.
s.isspace(): Tests whether a string comprises only whitespace characters.
s.startswith(subStr): Tests whether the string s starts with the string subStr, passed as a parameter.
s.endswith(subStr): Tests whether the string s ends with the suffix subStr, passed as a parameter.
● s.isupper(): The method isupper() tests whether the string comprises only uppercase characters. For
example,
>>> msg1 = 'SURAIYAJABIN'
>>> msg1.isupper()
True
>>> msg2 = 'SuraiyaJabin'
>>> msg2.isupper()
False
As the string msg1 comprises all uppercase letters, when the method isupper() is applied to it, the method
returns True. However, as string msg2 comprises a mix of uppercase and lowercase characters, when the method
isupper() is applied to it, the method returns False.
● s.islower()The method islower() tests whether the string comprises only lowercase characters, For example,
>>> msg1 = 'suraiyajabin'
>>> msg1.islower()
True
>>> msg2 = 'SuraiyaJabin'
>>> msg2.islower()
False
As the string msg1 comprises all lowercase letters, when the method islower() is applied to it, the method
returns True. However, since the string msg2 comprises a mix of uppercase and lowercase characters, when the
method islower() is applied to it, the method returns False.
● s.isalpha(): The method isalpha() checks whether a string comprises only alphabets, for example,
>>> 'SuraiyaJabin'.isalpha()
True
>>> 'Suraiya Jabin'.isalpha()
False
In the first function call, the method isalpha() returned True as the string comprises only the alphabets of the
English language. However, the second function call returned False because the string included a space.
● s.isdigit(): The method isdigit() checks whether a string comprises only digits. For example,
>>> '9898989898'.isdigit()
True
The above function call returns True since the string '9898989898' is composed of digits only. However, the
following function call returns False as the string includes the symbols + and -, apart from the digits.
Strings 265

