Differentiate between the following:
i) find() and index()
find() index()
• It returns the lowest index in the string • It returns the lowest index where the
where the substring sub is found specified substring is found.
within the slice range of start and end.
• It returns 1 if the substring is not
found.
• Syntax:
<string>.find(sub[,st[,end]]) • Syntax:
• Example: <string>.index(sub[,start[,end]])
string= ‘it goes as- ringa ringa roses’ • Example:
sub= ‘ringa’ ‘abracadabra’.index(‘ab’)
string.find(sub) o/t
o/t 0
13
ii) title() and capitalize()
title() capitalize()
• It returns a title cased version of the • It returns a copy of string with its first
string where all words start with character capitalized.
uppercase characters and all
remaining letters are lowercase.
• Syntax:
<string>.title() • Syntax:
• Example: <string>.capitalize()
“the sippo app”.title() • Example:
o/t ‘true’.capitalize()
The Sippo App o/t
True
iii) split() and partition()
split() partition()
• It splits a string based on given • The partition() method split the string
string or character and returns a at the first occurance of separator and
list. returns a tuple containing three items.
• Syntax:
• Syntax: <string>.partition(<separator/string>)
<string>.split(<string/char>) • Example:
• Example: txt="I enjoy working in Python"
print("I love Python".split()) x=txt.partition("working")
print(x)
o/t
o/t ('I enjoy ', 'working', ' in Python')
['I', 'love', 'Python']
iv) upper() and lower()
upper() lower()
• It returns a copy of the string • It returns a copy of the string
converted to uppercase. converted to lower case.
• Syntax: • Syntax:
<string>.upper() <string>.lower()
• Example: • Example
string.upper() string.lower()
‘HELLO’ ‘hello’
string2.upper() string2.lower()
‘THERE’ ‘there’
string3.upper() string3.lower()
‘GOLDY’ ‘goldy’
string4.upper() string4.lower()
‘U123’ ‘123f’
string5.upper()
‘123F’
v) startswith() and endswith()
startswith() endswith()
• Returns True if the string starts with • Returns True if the string ends with
the substring sub, otherwise returns the substring sub, otherwise returns
False. False.
• Syntax: • Syntax:
<string>.startswith() <string>.endswith()
• Example: • Example:
“abcd”.startswith(“cd”) “abcd”.endswith(“b”)
o/t o/t
False False
vi) isalpha() and isdigit()
isalpha() isdigit()
• It returns True if all the characters in • It returns True if all the characters in
the string are alphabetic and there is the string are digits. There must be
atleast one character, False atleast one character, else returns
otherwise. False.
• Syntax: • Syntax:
<string>.isalpha() <string>.isdigit()
• Example: • Example:
• string.isalpha() string.isdigit()
o/t o/t
False False
vii) lstrip() and rstrip()
lstrip() rstrip()
• Returns a copy of the string with • Returns a copy of the dtring with
leading whitespaces removed, ie, leading and trailing white spaces
whitespaces from the rightmost removed, ie, white spaces from the left
ends are removed. most and rightmost ends removed.
• Syntax:
• Syntax: <string>.rstrip()
<string>.lstrip() • Example:
• Example: ‘sipo’.rstrip()
“sipo”.lstrip() o/t
o/t ‘ sipo’
‘sipo ‘