String Methods

 s="hari taraka prabhu welcome to python programming"

print("s value is:",s)

s value is: hari taraka prabhu welcome to python programming

=====================================================================
capitalize

print("------------->>> capitalize() <<<----------")
print("s.capitalize() is :", s.capitalize()) #1st letter make capital


------------->>> capitalize() <<<----------
s.capitalize() is : Hari taraka prabhu welcome to python programming

=====================================================================
center

s="hari taraka prabhu welcome to python programming"

print("s value is:",s)
s value is: hari taraka prabhu welcome to python programming


print("s.center(100) is :",s.center(100,"*"))
print("                           ")

s.center(100) is : **************************hari taraka prabhu welcome to python programming**************************



print("s.center(80,$) is :",s.center(80,"$"))
print("                           ")


                       
s.center(80,$) is : $$$$$$$$$$$$$$$$hari taraka prabhu welcome to python programming$$$$$$$$$$$$$$$$

=====================================================================
count

print(s)
hari taraka prabhu welcome to python programming


s.count('o')
4


s.count('o',8,len(s))
4


s.count('o',24,len(s))
3


Note: #8,24 is starting positions of given string
=====================================================================
endswith

print(s)

hari taraka prabhu welcome to python programming

s.endswith("ing")
True


s.endswith("thon")
False
=====================================================================
startswith

print(s)
hari taraka prabhu welcome to python programming

s.startswith("har")
True


s.startswith("Har")
False


s.startswith("pyt",24,len(s))
False


s.startswith("pyt",29,len(s))
False


s.startswith("pyt",31,len(s))
False


s.startswith("pyt",30,len(s))
True
=====================================================================
find

print(s)

hari taraka prabhu welcome to python programming


s.find('o')
23
#from 1st occurance


s.find('t')
5
=====================================================================
rfind

print(s)
hari taraka prabhu welcome to python programming

s.rfind('o')
39


s.rfind('t')
32
#from reverse of 1st occurance
=====================================================================
index

print(s)
hari taraka prabhu welcome to python programming


s.index('o')
23


s.rindex('o')
39
=====================================================================
print(s)
hari taraka prabhu welcome to python programming

#Index & find difference

s.find('z')

-1


s.index('z')
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    s.index('z')
ValueError: substring not found
=====================================================================
isalnum

l="abc123"

print(l)
abc123


l.isalnum()
True


l="abc"
print(l)
abc


l.isalnum()
True


l="123"
print(l)
123


l.isalnum()
True


l="abc@123"
print(l)
abc@123


l.isalnum()
False
=====================================================================
isalpha


l="abc"
print(l)
abc


l.isalpha()
True

l="123"
print(l)
123


l.isalpha()
False
=====================================================================
isdigit

s="123"
print(s)
123


s.isdigit()
True

s="abc"
print(s)
abc


s.isdigit()
False
=====================================================================
isspace

s=" "
print(s)
 
s.isspace()
True


s="abc def"
print(s)
abc def


s.isspace()
False
=====================================================================
islower


s="abc"
s.islower()
True


s="Abc"
s.islower()

False

s="ABC"
s.isupper()

True


s="abc"
s.isupper()

False
=====================================================================
istitle

s="hari taraka prabhu welcome to python programming"

s.title()
'Hari Taraka Prabhu Welcome To Python Programming'
#1st letter will make capital letter for all words


s.istitle()
False

s="Hari Taraka Prabhu Welcome To Python Programming"
s.istitle()
True
=====================================================================
ljust

s="hari taraka prabhu welcome to python programming"

s.ljust(100," ")
'hari taraka prabhu welcome to python programming                                                    '


s.ljust(100,"8")
'hari taraka prabhu welcome to python programming8888888888888888888888888888888888888888888888888888'


s.rjust(100,"8")

'8888888888888888888888888888888888888888888888888888hari taraka prabhu welcome to python programming'

=====================================================================
s="hari taraka prabhu"

s.upper()
'HARI TARAKA PRABHU'


s.lower()

'hari taraka prabhu'
=====================================================================
lstrip

s=("           hari taraka prabhu           ")
   
s.lstrip()
'hari taraka prabhu           '


s.rstrip()
'           hari taraka prabhu'
=====================================================================
replace

s="hari taraka prabhu"

s.replace("a","B")
'hBri tBrBkB prBbhu'


s.replace("taraka","MANYAN")
'hari MANYAN prabhu'


s.replace("a","z",2)
'hzri tzraka prabhu'
=====================================================================
split

hari taraka prabhu

s.split()
['hari', 'taraka', 'prabhu']


s.split(" ",1)
['hari', 'taraka prabhu']


s.split("a")
['h', 'ri t', 'r', 'k', ' pr', 'bhu']
=====================================================================
swapcase

print(s)
HaRi TaRAkA PraBHu


s.swapcase()
'hArI tAraKa pRAbhU'
=====================================================================
zfill

print(s)
HaRi TaRAkA PraBHu


s.zfill(100)
'0000000000000000000000000000000000000000000000000000000000000000000000000000000000HaRi TaRAkA PraBHu'