Strings and Characters
Strings and Characters
CHARACTERS
Creating strings
Ex1:
s='BEC BGK'
print(s)
print(s[1])
s[1]=‘k’
Example: Write a python program to read two strings and check are
they equal or first string is greater than second or smaller. Print
appropriate messages
Removing space from string
A space is also considered as a character inside a string. Sometime
unnecessary spaces in a string lead to wrong result.
if ‘bec ’==‘bec’:
print(“equal”)
else:
print(“not equal”)
Removing space from string
Python provides following functions
p=ms.find(ss,0,len(ms))
if p==-1:
print("Not prsent")
else:
print('prsent at ',p)
p=ms.index(ss,0,len(ms))
if p=='ValueError':
print("Not prsent")
else:
print('prsent at ',p)
p=ms.rfind(ss,0,len(ms))
if p==-1:
print("Not prsent")
else:
print('prsent at ',p)
p=ms.rindex(ss,0,len(ms))
if p=='ValueError':
print("Not prsent")
else:
print('prsent at ',p)
Python program to display all positions of a sub string in a given main string
ms=input("Enter main string:")
ss=input("Enter the sub string:")
i=0
f=False
l=len(ms)
while i<l:
p=ms.find(ss,i,l)
if p!=-1:
print(ss,' is found at',p+1)
i=p+1
f=True
else:
i=i+1
if f==False:
print('Substring not found:')
Counting substring in a string
count() is available to count the number of occurrences of a sub string in a
main string
stringname.count(substring)
Returns an integer number that represents how many times the substring
is found in main string if not found then returns zero
stringname.count(substring,beg,end)
Returns an integer number that represents how many times the substring
is found in main string from beginning to end position
If substring is not found in main string then returns zero
Counting substring in a string
s='BEC BGK CSE BEC'
print(s.count('EC'))
print(s.count('E'))
print(s.count('B',3,len(s)))
print(s.count('S',11,len(s)))
print(s.count('ISE'))
Split() splits a string into pieces. These pieces are returned as a list.
Example:split a string wherever , occurs in a given string
Syntax:
stringname.split(splitting character)
S=“one,two,three,four”
S1=S.split(‘,’)
Print(s1)
output
['one', 'two', 'three', 'four’]
Example: python program to read a string of numbers separated by
space. Split the string at space. Print the numbers and find their sum
join()
Join() does the reverse of split(). Joins the string elements of list or tuple using
a separator
separator.join(list/tuple)
separator represents the character to be used to join the elements of string in a
list or tuple
l=['A','B','C']
k=''.join(l)
print(k)
Output
ABC
Example: Read a list of numbers in a word and join these words using
underscore and print final string
Changing case of a string:
swapcase(),title(),upper(),lower()
s=‘Python is the future’
print(s.upper())
PYTHON IS THE FUTURE
print(s.lower())
python is the future
print(s.swapcase())
PYTHON IS THE FUTURE
Print(s.title())
Python Is The Future
String Testing methods
isalnum() –returns True if all characters in string are alphanumeric(a-
z,A-Z,0-9) otherwise False
S=“delhi98”
print(S.isalnum())
isalpha()-returns True if string has all characters alphabetic(A-Z,a-z)
S=‘d111’
print(s.isalpha())
Isdigit()-returns True if string contains only numeric digits(0-9)
otherwise False
s=‘111’
print(s.isdigit())
Contd..