string
string
MODULE - 27
1
Contents of Module 27
String Methods and Built-in Functions
len()
Returns the length of the given string passed as the argument.
>>> str="this is my book"
>>>len(str)
15
capitalize()
Return a string with its first character capitalized and the rest
lower cased.
>>> str="this Is stRing example .... wow!!!" -
>>> str.capitalize()
'This is string example....wow!!!'
Distance Learning Programme through
3
E-module by AEES, Mumbai
String Methods and Built-in Functions
title ()
Return a title cased version of the string, where words start with an
uppercase character and the remaining characters are lowercase. It will
return unexpected result in cases where words have apostrophe etc.
>>> str="this is string example .... wow!!!"
>>> str.title()
'This Is String Example....Wow!!!'
>>> str="this isn't a float example .... wow!!!"
>>> str.title ()
"This Isn'T A Float Example .... Wow!!!"
upper ()
Return a string with all the cased characters converted to uppercase.
>>> str="this is string example .... wow!!!"
>>> str.upper()
'THIS IS STRING EXAMPLE....WOW!!!'
count(sub[,start[,end]])
Return the number of non-overlapping occurrences of sub-string sub in the
range [start, end]. Optional arguments start and end are interpreted as in
slice notation.
>>> str="this is string example .... wow!!!"
>>> sub="i"
>>> str.count(sub,4,40)
2
>>> sub="wow"
>>> str.count(sub)
1
Distance Learning Programme through
5
E-module by AEES, Mumbai
String Methods and Built-in Functions
find(sub[,start[,end]])
Return the lowest index in the string where sub-string sub is found, such that sub is contained in the slice s [ start: end].
Optional arguments start and end are interpreted as in slice notation. Return -1, if sub is not found.
>>> str1 = "this is string example .... wow! !!"
>>> str2="exam"
>>> str1.find(str2)
15
>>> str1.find(str2,2,10)
15
>>> strl.find(str2,40)
-1
index(sub[,start[,end]])
Like find (), but raise ValueError when the sub-string is not found.
>>> str1="this is string example....wow!!!"
>>> str2="exam"
>>> str1.index(str2)
15
>>> str1.index(str2,10) .
15
>>> str1.index(str2,40)
Traceback (most recent call last):
File "<pyshell#38>", line 1, in str1.index(str2, 40)
ValueError: substring not found
isalpha()
Return True, if all characters in the string are alphabetic, otherwise False is returned.
>>> str="this"
>>> str.isalpha()
True
>>> str="this is string example .... wow!!!"
>>> str.isalpha() ,
False
isspace()
Return True, if there are only whitespace characters in the string, otherwise False is returned.
>>> str=" "
>>> str.isspace ()
True
>>> str="This is string example .... wow!!!"
>>> str.isspace ()
False
islower()
Return True, if all cased characters in the string are in lowercase and there is at least one cased
character, False otherwise.
>>> str="THIS is string example....wow!!!"
>>> str.islower()
False
>>> str="this is string example .... wow!!!"
>>> str.islower()
True
>>> str="this2009"
>>> str.islower()
True
>>> str="2009"
>>> str.islower()
False
istitle ()
Return True, if the string is title cased, otherwise False is returned.
>>> str="This Is String Example ... Wow!!!"
>>> str.istitle ()
True
>>> str="This is string example .... wow!!!"
>>> str.istitle()
False
Distance Learning Programme through
10
E-module by AEES, Mumbai
String Methods and Built-in Functions
strip(chars)
Return a string with the leading and trailing characters removed. The chars
argument is a string specifying the set of characters to be removed. If
omitted or None, the chars argument defaults to removing whitespace.
>>> str="OOOOOOOthis is string example... .wow! !!0000000"
>>> str.strip ('0')
'this is string example....wow!!!'
partition(sep)
Split the string at the first occurrence of sep, and return a tuple containing
the part before the separator, the separator itself, and the part after the
separator.
>>> str=" this is string example .... wow!! ! "
>>> str.partition('s')
(' thi', 's', ' is string example .... wow!! ! ')
Distance Learning Programme through
11
E-module by AEES, Mumbai
String Methods and Built-in Functions
split(sep)
Return a list of the words from the string using sep as the delimiter string. If
sep is not specified or None, any whitespace string is a separator.
>>> str='this is my book'
>>> str.split()
['this', 'is', 'my', 'book']
swapcase()
Return a copy of the string with reversed character case.
>>> str="This is string example .... WOW!!! "
>>> str.swapcase()
'tHIS IS STRING EXAMPLE ... .wow! !! '
join(separater)
Return a string in which the string elements have beeb joined by a separator.
>>> str="hello"
>>> t='*'
>>> t.join(str)
'h*e*l*l*o'
replace(old,new)
Return a string with all occurrences of sub-string old replaced by new.
>>> str="this is string example.... wow!! ! this is really string"
>>> str.replace("is ","was")
'thwas was string example.... wow!! ! thwas was really string'
Rani Sasty
PGT(ss)
AECS-4,Mumbai