Chapter 5 Engstring Manipulation
Chapter 5 Engstring Manipulation
By: G.Hota
DAV PKT
Introduction
• As we know that a sequence of characters enclosed in
single quotes, double quotes or triple quotes
(‘ ‘ , “ “, ‘’’ ‘’’) is called a string.
• In python, strings are immutable meaning they can’t be
changed.
• In a String, each character remains at a unique position
number or index number which goes from 0 to n-1 (n is
the total number of characters in the string).
• In this chapter, we will see techniques of string
manipulation.
String Creation
• String can be created in following ways-
1. By assigning value directly to the variable
String Literal
2. By taking
Input
Output
String Operators
• There are 2 operators that can be used to work
upon strings + and *.
»+ (it is used to join two strings)
• Like - “tea” + “pot” will result into “teapot”
• Like- “1” + “2” will result into “12”
• Like – “123” + “abc” will result into “123abc”
Word R E S P O N S I B I L I T Y
-14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Reverse index
word =
“RESPO
NSIBILIT
Y”
word[ 0 :
14 ] will
result
into‘RES
PONSIBI
LITY’
word[ 0 : 3] will result into‘RES’
word[ 2 : 5 ] will result into‘SPO’
String Slicing…….
Name=“AMAZING”
Name[0:7] -> AMAZING
Name[0:3] -> AMA
Name[2:5] -> AZI
Name[-7:-3] -> AMAZ
Name[-5:-1] -> AZIN
Name[:7] -> AMAZING
Name[:5] -> AMAZI
Name[3:] -> ZING
Name[:3] + Name[3:] -> AMAZING
Name[:-7] + Name[-7:] -> AMAZING
Name[1:6:2] -> MZN
Name[-7: -3 : 3] -> AZ
Name[::-2] -> GIAA
Name[::-1] -> GNIZAMA
Print(s[7]) -> ERROR print (Name[6:8]) -> 0 empty string
PRINTING PATTERN WITHOUT
NESTED LOOP
String=@ @
Pattern=“ ” @@
for a in range(5): @@@
Pattern+=String @@@@
Print(pattern) @@@@@
STRING FUNCTIONS AND
METHODS Method
Description
Example
len()
Returnsthe given string length of the
>>> str1 = 'Hello World!'
>>> len(str1) 12
title()
Returns the string with first letter of every word in the string in uppercase and rest in
lowercase
>>> str1 = 'hello WORLD!'
WAP THAT READS THE LINE AND PRINT THE FOLLOWING STATISTICS
Print the no of uppercase letter
Lower case letter
Number of Alphabets
Number of digits
STRING FUNCTIONS AND
METHODS
lower()
Returns the string with all uppercase letters converted to lowercase
>>> str1 = 'hello WORLD!'
>>> str1.lower() 'hello world!'
upper()
Returns the string with all lowercase letters converted to uppercase
>>> str1 = 'hello WORLD!'
>>> str1.upper() 'HELLO WORLD!'
count(str, start, end)
Returns number of times substring str occurs in the given string. If we
do not give start index and end index then searching starts from index 0
and ends at length of the string
Returns the first occurrence of index of substring str occurring in the given
string. If we do not give start and end then searching starts from
index 0 and ends at length of the string. If the substring is not present in
the
given string, then the function
>>> str1.find('Hello',10,20)
13
>>> str1.find('Hello',15,25)
19
>>> str1.find('Hello’)
0
returns -1
>>> str1.find('Hee')
-1
STRING FUNCTIONS AND METHODS
index(str, start, end)
Same as find() but raises an exception if the substring is not present
in the given string
string=input(“Enter a string”)
count=3
else:
string=string[:2] 4
count+=1
break
2) aaccbb
print(string)
print(count)
ccbb 3
CAPITALIZE THE FIRST LETTER OF EACH WORD AND
FORMS A NEW STRING
string=input(“Your String”)
Length=len(string)
a=0
End=Length
string2=‘’ #Empty string
while a<Length:
if a==0:
string2+=string[0].upper()
a+=1
elif (string[a]==‘ ‘ and string[a+1]!=‘ ‘):
string2+=string[a]
string2+=string[a+1].upper()
a+=2
else:
string2+=string[a]
a+=1
print(“Original String :”,string)
print(“Capitalized words string”,string2)
CHECK WHETHER A STRING IS A
PALINDROME OR NOT
string=input(“Your String”)
Length=len(string)
Mid=length/2
Rev=-1
for a in range(mid):
if string[a]==string[rev]:
a+=1
rev-=1
else:
print(string,” Is not a palindrome”)
break
else:
print(string,”is a palindrome”)
CAPITALIZE EVERY OTHER LETTER IN
THE STRING passion -> pAsSiOn
string=input(“Your String”)
Length=len(string)
Print(“Original String : “,string)
string2=“ ”
for a in range (0,length,2):
string2+=string[a]
If a<(length-1):
string2+=string[a+1].upper()
print(“Alternatively Capitalized String:” , string2)
PRINT THE LONGEST SUBSTRING
string=input(“Your String”)
Length=len(string)
maxlen=0
maxsub=‘’
Sub=‘’
Lensub=0
for a in range(length):
if string[a] in ‘aeiou’ or string[a] in ‘AEIOU’:
if lensub>maxlen:
maxsub=sub
maxlen=Lensub
sub=‘ ’
Lensub=0
else:
sub+=string[a]
lensub=len(sub)
a+=1
print(“Maximum Length Consonant String is :”,maxsub, end=‘ ‘)
print(“with”, maxlength , “characters”)
Assignment
1. WAP to print the following pattern
1. INDIA 2. I
INDI IN
IND IND
IN INDI
I INDIA
2. WAP to search a substring from a given line of string.
www.pythontrends.wordpress.com