String methods & built in functions: 12th cbse

String methods & built in functions:

len() capitalize() find(sub[,start[, end]]) isalnum() isalpha()
isdigit() lower() islower() isupper() upper() lstrip()
rstrip() isspace() istitle() replace(old,new) join ()
swapcase() partition(sep) split([sep[,maxsplit]])

Example:
>>> s='Congratulations'
>>> len(s)
15
>>> s.capitalize()
'Congratulations'
>>> s.find('al')
-1
>>> s.find('la')
8
>>> s[0].isalnum()
True
>>> s[0].isalpha()
True
>>> s[0].isdigit()
False
>>> s.lower()
'congratulations'
>>> s.upper()
'CONGRATULATIONS'
>>> s[0].isupper()
True
>>> s[1].isupper()
False
>>> s.replace('a','@')
'Congr@tul@tions'
>>> s.isspace()
False
>>> s.swapcase()
'cONGRATULATIONS'
>>> s.partition('a')
('Congr', 'a', 'tulations')
>>> s.split('ra',4)
['Cong', 'tulations']
>>> s.split('a')
['Congr', 'tul', 'tions']
>>> a=' abc '
>>> a.lstrip()
'abc '
>>> a.rstrip()
' abc'
Examples:

Example: Write a program to input any string and count number of uppercase and lowercase letters.
Code:
s=raw_input("Enter any String")
rint s
u=0
l=0
i=0
while i<len(s):
if (s[i].islower()==True):
l+=1
if (s[i].isupper()==True):
u+=1
i+=1
print "Total upper case letters :", u
print "Total Lower case letters :", l

Output:
Enter any String Python PROG
Python PROG
Total upper case letters: 5

Total Lower case letters: 5

No comments:

Post a Comment