0% found this document useful (0 votes)
25 views14 pages

String Method

Uploaded by

RIZWAN N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views14 pages

String Method

Uploaded by

RIZWAN N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 14

ZION AND ALWIN GROUP OF SCHOOLS

STRING METHODS AND FUNCTIONS


RETURNS
S.NO. FUNCTION /METHOD DESCRIPTION EXAMPLE
VALUE /NOT

returns length of the string ie) s="Welcome_Python3.5" print(len(s))


number of characters in the count=len(s)
1 len() string yes print(count)

returns copy of the string with s="wElCoMe_PyThOn3.5"


first character capitalized. print(s.capitalize())
s1=s.capitalize()
2 capitalize() yes print(s1)
print(s)

returns number of times s="welcome to python3.5"


substring is present in the string print(s.count('o'))
o_count=s.count('o')
3 count() yes print(o_count)
print(s)

returns the lowest index in the s="welcome to python3.5"


string where substring is print(s.find('o'))
present.Returns -1 if substring is o_find=s.find('o')
4 find(sub,[start],[stop]) not found. yes print(o_find)
print(s)

returns the lowest index where the s="welcome to python3.5"


substring is present. Returns print(s.index('o'))
ValueError if substring is not o_find=s.index('o')
5 index(sub,[start],[stop]) yes
present. print(o_find)
print(s)
RETURNS
S.NO. FUNCTION /METHOD DESCRIPTION EXAMPLE
VALUE /NOT
1. returns True if all the characters s1="welcomepython3"
in the string are alphabets or digits. s2=""
2. returns False if string is s3="[email protected]"
empty. 3. returns print(s1.isalnum())
False if any one character in the print(s2.isalnum())
6 isalnum() string is special character(space is yes print(s3.isalnum())
also special character)

1. returns True if all the characters s1="welcomepython3"


in the string are only digits. s2=""
2. returns False if string is s3="[email protected]"
empty. 3. returns s4="12345"
False if any one character in the print(s1.isdigit())
string is special character or print(s2.isdigit())
7 isdigit() alphabet(space is also special yes print(s3.isdigit())
character) print(s4.isdigit())

1. returns True if all the characters s1="welcomepython3"


in the string are only alphabets. s2=""
2. returns False if string is s3="[email protected]"
empty. 3. returns s4="12345"
False if any one character in the s5="ziongroup"
string is special character or print(s1.isalpha())
8 isalpha() digit(space is also special character) yes print(s2.isalpha())
print(s3.isalpha())
print(s4.isalpha())
print(s5.isalpha())
RETURNS
S.NO. FUNCTION /METHOD DESCRIPTION EXAMPLE
VALUE /NOT
1. returns True if all the characters s1="WELCOMEHOME"
in the string are only lower case s2=""
alphabets or atleast one lowercase s3="[email protected]"
character should be present. s4="12345"
2. returns False if string is s5="ziongroup"
empty. 3. returns s6="@#$%a"
False if all the character in the s7="ziongrouP"
string are special characters or print(s1.islower())
9 islower() digits or uppercase(space is also yes print(s2.islower())
special character) print(s3.islower())
print(s4.islower())
print(s5.islower())
print(s6.islower())
print(s7.islower())

1. returns True if all the characters s1="WELCOMEHOME"


in the string are only upper case s2=""
alphabets or atleast one upper case s3="[email protected]"
character should be present. s4="12345"
2. returns False if string is s5="ZIoN"
empty. 3. returns s6="@#$%A"
False if all the character in the print(s1.isupper())
10 isupper() string are special characters or yes print(s2.isupper())
digits or lowercase(space is also print(s3.isupper())
special character) print(s4.isupper())
print(s5.isupper())
print(s6.isupper())
RETURNS
S.NO. FUNCTION /METHOD DESCRIPTION EXAMPLE
VALUE /NOT
1. returns True if all the characters s1="WELCOMEHOME"
in the string are only digits. s2=""
2. returns False if string is s3="[email protected]"
empty. 3. returns s4="12345"
False if the character in the string s5="@#$%90"
are special characters or print(s1.isdigit())
11 isdigit() alphabets(space is also special yes print(s2.isdigit())
character) print(s3.isdigit())
print(s4.isdigit())
print(s5.isdigit())

1. returns a copy of the string s1="WELCOMEHOME"


converted to lower case. s2=""
2. ignores digits and special s3="[email protected]"
characters s4="12345"
s5="@#$%90"
print(s1.lower())
12 lower() yes print(s2.lower())
print(s3.lower())
print(s4.lower())
print(s5.lower())
RETURNS
S.NO. FUNCTION /METHOD DESCRIPTION EXAMPLE
VALUE /NOT
1. returns a copy of the string s1="WELcomeHOME"
converted to upper case. s2=""
2. ignores digits and special s3="[email protected]"
characters s4="12345"
s5="@#$%90"
print(s1.upper())
print(s2.upper())
13 upper() yes print(s3.upper())
print(s4.upper())
print(s5.upper())

returns copy of the string with s1=" WELcome HOME "


leading white spaces removed. s2=" "
s3="[email protected] "
print(s1.lstrip(),end="$")
14 lstrip() yes print(s2.lstrip(),end="$")
print(s3.lstrip())

returns copy of the string with s1=" WELcome HOME "


trailing white spaces removed. s2=" "
s3="[email protected] "
print(s1.rstrip(),end="$")
15 rstrip() yes print(s2.rstrip(),end="$")
print(s3.rstrip())
RETURNS
S.NO. FUNCTION /METHOD DESCRIPTION EXAMPLE
VALUE /NOT
returns copy of the string with s1=" WELcome HOME "
trailing and leading white spaces s2=" "
removed. s3="[email protected] "
print(s1.strip(),end="$")
16 strip() yes print(s2.strip(),end="$")
print(s3.strip(),end="$")

returns the copy of the string with s1="red colour blue colour"
all the occurances of old string print(s1.replace("colour","color"))
17 replace() replaced by new string yes print(s1.replace(" ","@#"))

1. joins the string or character after s1="red colour blue colour"


each member of the sequence and l1=['red','blue','green']
returns it. 2. If the l2=[23,'abi','pass']
sequence is tuple or list all the print('#'.join(s1))
members must be strings only. print(' color '.join(l1))
18 join() yes print(' color '.join(l2))

It splits the string and returns as a s1="red blue green"


list cotaining split string as member s2="z$i$p$s#a$m$p$s#a$i$p$s"
of the list. print(s1.split())
19 split() yes
print(s2.split("#"))
RETURNS
S.NO. FUNCTION /METHOD DESCRIPTION EXAMPLE
VALUE /NOT
it splits the string only at the first s1="red blue green"
occurance of the separator and s2="z$i$p$s##a$m$p$s#a$i$p$s"
returns the tuple with three print(s1.partition(" "))
elements only.(part before print(s2.partition("#"))
20 partition() separator,separatorand part after yes
separator)

returns True ifthe string starts with s1="red blue green"


substring or character otherwise print(s1.startswith(" "))
False. print(s1.startswith("r"))
21 startswith() yes
print(s1.startswith("red"))
print(s1.startswith("Red"))

returns True ifthe string ends with s1="red blue green"


substring or character otherwise print(s1.endswith(" "))
False. print(s1.endswith("n"))
22 endswith() yes
print(s1.endswith("red"))
print(s1.endswith("green"))

returns the title cased version of the s1="i love my India"


string ie) all the words start with s2="python java c++"
uppercase and remaining characters print(s1.title())
23 title() yes
in lower case. print(s2.title())

returns True if title cased version of s1="i love my India"


the string ie) all the words start with s2="Python Java C++"
uppercase and remaining characters print(s1.istitle())
24 istitle() in lower case otherwise False. yes print(s2.istitle())
HOOLS
UNCTIONS
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT

You might also like