Text Handling
Text Handling
Description: Python programming can be used to process text data for the requirements in various
textual data analysis.
a) Demonstrate the following functions/methods which operates on strings in Python with suitable
examples: i) len( ) ii) strip( ) iii) rstrip( ) iv) lstrip( ) v) find( ) vi) rfind( ) vii) index( ) viii) rindex() ix)
count( ) x) replace( ) xi) split( ) xii) join( ) xiii) upper( ) xiv) lower( ) xv) swapcase( ) xvi) title( ) xvii)
capitalize( ) xviii) startswith() xix) endswith()
i.len(): We can use len() function to find the number of characters present in the string.
22
Removing spaces from the string: To remove the blank spaces present at either beginning and end of
the string, we can use the following 3 methods:
1. rstrip() ===>To remove blank spaces present at end of the string (i.e.,right hand side)
2. lstrip()===>To remove blank spaces present at the beginning of the string (i.e.,left hand side)
iii.rstrip(): Used to remove blank spaces present at end of the string (i.e.,right hand side)
capital=input("Enter your State Name:")
scapital=capital.strip()
if scapital=='Maharashtra':
print("Maharashtra ..Mumbai")
elif scapital=='Madras':
print("Madras...Chennai")
elif scapital=="Karnataka":
print("Karnataka...Bangalore")
else:
print("your entered city is invalid")
Output:
Enter your State Name:Maharashtra
Maharashtra ..Mumbai
iv) lstrip(): Used to remove blank spaces present at the beginning of the string (i.e.,left hand side)
Finding Substrings:
If you want to find whether the substring is available in the given string or not in Python, we have 4
methods.
v) v.find():
Returns index of first occurrence of the given substring. If it is not available then we will get -1
23
-1
29
vi)rfind():
23
51
47
46
vii) index():
index() method is exactly same as find() method except that if the specified substring is not available
then we will get ValueError
s = 'abbaaaaaaaaaaaaaaaaabbababa'
print(s.index('bb'))
output
viii) rindex():
s = 'abbaaaaaaaaaaaaaaaaabbababa'
print(s.rindex('bb'))
Output:
20
ix) count(): We can find the number of occurrences of substring present in the given string by using
count() method.
2. s.count(substring, begin, end) ===> It will search from begin index to end-1 index
s="abcabcabcabcadda"
print(s.count('a')) #6
print(s.count('ab')) #4
print(s.count('a',3,7)) #2
output:
6
4
2
x) replace(): We can repalce a string with another string in python using a library function replace().
Syntax: s.replace(oldstring,newstring)
Here, inside 's', every occurrence of oldstring will be replaced with new string
xi) split():
We can split the given string according to specified seperator by using split() method.
We can split the given string according to specified seperator in reverse direction by using rsplit()
method.
s="cse-ece-eee"
l=s.split('-')
for x in l:
print(x)
OUTPUT
cse
ece
eee
xii) join(): We can join a group of strings(list or tuple) with respect to the given seperator.
l=['ENJOY','THE','LITTLE','THINGS']
s=' # '.join(l)
print(s)
OUTPUT:
xiii) upper(): Used to convert all characters to upper case in the given string.
xiv) lower(): Used to convert all characters to lower case in the given string.
xv) swapcase(): Used to convert all lower case characters to upper case and all upper case characters to
lower case in the given string.
xvi) title(): Used to convert all characters to title case. (i.e first character in every word should be upper
case and all remaining characters should be in lower case in the given string).
xvii) capitalize(): Only first character will be converted to upper case and all remaining characters can be
converted to lower case
s='do things that Makes you Happy'
print(s.upper())
print(s.lower())
print(s.swapcase())
print(s.title())
print(s.capitalize())
output:
True
True
False
b) Write a Python Program to display all positions of substring in a given main string.
c) Write a program to use split and join methods in the given string and trace a birthday with a
dictionary data structure
d) Write a Regular Expression to represent all 10 digit mobile numbers and Write a Python Program to
check whether the given number is valid mobile number or not?
Regular Expression:
Program :
import re
s = input('Enter Number :')
m = re.fullmatch('[7-9][0-9]{9}',s)
if m!= None:
print(s,'is valid Mobile number')
else:
print(s,'is not valid Mobile number')
output:
e) Write a Python Program to check whether the given mail id is valid gmail id or not?
import re
s=input("Enter Mail id:")
m=re.fullmatch("\w[a-zA-Z0-9_.]*@gmail[.]com",s)
if m!=None:
print("Valid Mail Id");
else:
print("Invalid Mail id")
output: