String Functions
String Functions
Its holds alphabets or digits or special charecters, starting with index (location) 0
and ending with n-1.
(or) start with -1 in reverse
A=”Welcome”
Print(A[-1])
A[0]=w
A[2]=l
A[-1]=e
A[-2]=m
print(str1[3]) c
print(str1[-1]) n
print(str1[-2]) o
print(str1[3:6]) com
print(str1[3:7:2]) cm
print(str1[11:16]) learn
print(str1[11:16:4]) ln
print(str1[11:16:2]) lan
A="Welcome"
print(A+" to all") Welcome to all
print(5+2) 7
print("5"+"2") 52
print(5*3) 15
print("5"*3) 555
Sno Method Description Example OUTPUT
g
col="green" r
for x in col: e
print(x) e
n
x = txt.replace("bananas",
"apples")
print(x)
split(“val”) str1 = "My school"
The split() method
splits a string into a str2 = str1.split(" ")
list.
print(str1) My school
You can specify the
separator, default print(str2) ["My","school"]
separator is any
whitespace. print(str2[0]) My
print(str2[1]) School
str1 = "this is wisdom"
str2 = str1.split("is")
print(str2[0]) th
print(str2[1]) “”
print(str2[2]) _w
print(str2[3]) dom
str1 = "My school is near My
to metro"
school
str2=str1.split(" ")
is
for i in str2:
near
print(i)
to
metro
print(n)
4
n=str1.count("to")
print(n)
1
https://docs.python.org/2.5/lib/string-methods.html
Some more String Functions O/P
a="abc#mail.com" 3
print(a.find(“#"))
b="abcmail.com" (If the location is not found , it
print(b.find(“#")) returns -1)
-1
a="abc@xyz@mail@com" ['abc', 'xyz', 'mail', 'com']
print(a.split("@"))
b="abc@xyz@mail@com" ['abc', 'xyz@mail@com']
print(b.split("@",1))
c="abc@xyz@mail@com" ['abc', 'xyz', 'mail@com']
print(c.split("@",2))