Built-in functions
Built-in functions
>>> vowelCount
4
The system responds with 4 as the value of the vowelCount, even though the
number of vowels in the search string ‘Encyclopedia’ is 5. As the character ‘E’ was
not included in the string vowels, it was not included in counting too. To include
the count of uppercase vowels also, we just need to include vowels in uppercase
also in the string vowels:
vowels = ‘AEIOUaeiou’
rest of the code remains the same.
2. functions find and rfind
Examine the string colors. Suppose we wish to find out whether 'red' is present as a
substring in the string colors. We can do so by using the function find that returns
the index of the first occurrence of string argument 'red' in colors:
finding index of first occurrence of a string in another string
>>> colors = 'green, red, blue, red, red,
green'
>>> colors.find('red')
7
To find the last occurrence of a string, we use the function rfind that scans the
string in reversed order from the right end of the string to the beginning:
finding index of the last occurrence of a string in another string
>>> colors.rfind('red')
23
If the function find fails to find the desired substring, it
returns -1:
>>> colors.find('orange')
-1
BUILD – IN FUNCTION
LANGUAGE’.swapcase()
‘Python is programming language’
4. functions islower, isupper, and istitle
The functions islower and isupper can be used to check if all letters in a string are
in lowercase or uppercase, respectively, for example:
checking case (lower / upper ) of a string
>>> ‘python’.islower()
True
>>> ‘Python’.isupper()
False
The function istitle returns True if a string S (comprising atleast one alphabet) is in
title case, for example:
checking whether the string is in title case
>>> ‘Basic Python Programming’.istitle()
True
>>> ‘Basic PYTHON Programming’.istitle()
False
>>> ‘123’.istitle()
False
>>> ‘Book 123’.istitle()
True
5. Function replace
The function replace allows to replace part of a string by another string. It takes
two arguments as inputs. The first argument is used to specify the substring that is
to be replaced. The second argument is used to specify the string that replaces the
first string. For example:
replacing a substring with another string
BUILD – IN FUNCTION
8. Function join
Python function join returns a string comprising elements of a sequence separated
by the specified Delimiter. For example,
joining a sequence of strings
>>> ‘ > ‘.join([‘I’, ‘am’, ‘ok’])
‘I > am > ok’
>>> ‘ ‘.join((‘I’, ‘am’, ‘ok’))
‘I am ok’
>>> ‘ > ‘.join(“’I’, ‘am’, ‘ok’”)
“’ > I > ‘ > , > > ‘ > a > m > ‘ > , > > ‘ >O > k > ‘”
In the first example, the sequence comprises three elements, namely, ‘I’, ‘am’, and
‘ok’, which are combined to form the string ‘I > am > ok’. In the Second example,
we use space as a delimiter instead of >. In the third example, each character in the
string “’I > am > ok’” is an element of the sequence of Characters in “’I > am >
ok’”.
9. Functions isspace, isalpha, isdigit, and Isalnum
The functions isspace, isalpha, isdigit, and isalnum enable us to check whether a
value is of the desire type. For example, we can check whether the name entered
by a user contains only alphabets as follows:
does a string comprises alphabets, digits, or whitespaces Only ?
>>> name = input(‘Enter your name : ‘)
Enter your name : Nikhil
>>> name.isalpha()
True
>>> name = input(‘Enter your name : ‘)
Enter your name : Nikhil Kumar
>>> name.isalpha()
BUILD – IN FUNCTION
False
Note that the blank character is not an alphabet. Similarly, to check the validity of
a mobile number, we may want it to be a string of length 10 comprising of digits
only. This can be achieved using functions
isdigit and len:
>>> mobileN = input(‘Enter mobile no : ‘)
Enter mobile no : 1234567890
>>> mobileN.isdigit() and len(mobileN)
== 10
True
Python function isspace is used to check if the string Comprises of all whitespaces:
check for a whitespace only string
>>> ‘ \n\t ‘.isspace()
True
The function isalnum checks whether a string comprises of alphabets and digits
only. For example, if the password is allowed to comprise of only alphabets and
digits, we may use the function isalnum to ensure this constraint for a user
specified password:
check for an alphanumeric string
>>> password = input(‘Enter password : ‘)
Enter password : Kailash107Ganga
>>> password.isalnum()
True
>>> password = input(‘Enter password : ‘)
Enter password : Kailash 107 Ganga
>>> password.isalnum()
False
BUILD – IN FUNCTION
'message'
One may also use alternative coding schemes such as utf8 and utf16.