String Functions
String Functions
len(str)
Returns the length (no of characters) of the string.
Example:
>>> A="Corporation"
>>> print(len(A))
Output:
11
capitalize( )
Used to capitalize the first character of the string
Example:
>>> city="chennai"
>>> print(city.capitalize())
Output:
Chennai
center(width, fillchar)
Returns a string with the original string centered to a total of width columns and filled with fillchar in columns that do not have characters
Example:
>>> str1="Welcome"
>>> print(str1.center(15,'*') )
Output:
****Welcome****
find(sub[, start[, end]])
The function is used to search the first occurrence of the sub string in the given string. It returns the index at which the substring starts. It returns -1 if the substring does not occur in the string.
Example:
>>>str1=’mammals’
>>>str1.find(‘ma’)
Output:
0
On omitting the start parameters, the function starts the search from the beginning.
Example:
>>>str1.find(‘ma’,2)
Output:
3
Example:
>>>str1.find(‘ma’,2,4)
Output:
-1
Displays -1 because the substring could not be found between the index 2 and 4-1.
isalnum( )
Returns True if the string contains only letters and digit. It returns False. If the string contains any special character like _, @, #, *, etc.
Example;
>>>str1=’Save Earth’
>>>str1.isalnum()
Output:
False
The function returns False as space is an alphanumeric character.
Example:
>>>’Save1Earth’.isalnum()
Output:
True
isalpha( )
Returns True if the string contains only letters. Otherwise return False.
Example:
>>>’Click123’.isalpha()
Output:
False
Example;
>>>’python’.isalpha( )
Output:
True
isdigit( )
Returns True if the string contains only numbers. Otherwise it returns False.
Example:
>>> str1=’Save Earth’
>>>print(str1.isdigit( ))
Output:
False
lower( )
Returns the exact copy of the string with all the letters in lowercase.
Example:
>>>str1=’SAVE EARTH’
>>>print(str1.lower())
Output:
save earth
islower( )
Returns True if the string is in lowercase.
Example:
>>> str1=’welcome’
>>>print (str1.islower( ))
Output:
True
isupper( )
Returns True if the string is in uppercase.
Example:
>>> str1=’welcome’
>>>print (str1.isupper( ))
Output:
False
upper( )
Returns the exact copy of the string with all letters in uppercase.
Example:
>>> str1=’welcome’
>>>print (str.upper( ))
Output:
WELCOME
title( )
Returns a string in title case
Example:
>>> str1='education department'
>>> print(str1.title())
Output:
Education Department
swapcase( )
It will change case of every character to its opposite case vice-versa.
Example:
>>> str1="tAmiL NaDu"
>>> print(str1.swapcase())
Output:
TaMIl nAdU
Qus: What is a function? How is it useful?
A function is a named block in a program which has some certain working. Whenever you call this it performs all its working. It reduces the complexity and reduces the labour of writing same code again and again.
Qus: What is an argument? Give an example of one.
Ans : An argument is the requirement of a function to perform its working. For ex- pow(x,y), here x and y are the arguments which are required to pass to pow() for its working.
Qus. What is a string slice? How is it useful?
Ans: String Slice is a part of a string containing some contiguous characters from the string. It is accessed from the string by providing a range in ―[ ]‖ brackets i.e. S [n:m]. Python returns all the characters at indices n, n+1, n+2 . . . m-1 e.g. 'Barabanki'.[4:7] will return 'ban'.
Qus. Which functions would you chose to use to remove leading and trailing white spaces from a given string?
Ans: Python String strip() function will remove leading and trailing white spaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.
Qus. Suggest appropriate functions for the following tasks –
(a) To check whether the string contains digits.
(b) To find the occurrence a string within another string.
(c) To convert the first letter of a string to upper case.
(d) To convert all the letters of a string to upper case.
(f) To check whether all the letters of the string are in capital letters.
(g) to remove all the white spaces from the beginning of a string.
Ans. (a) isalnum() (b) find() (c) capitalize() (d) upper() (f) isupper() (g) lstrip()