0% found this document useful (0 votes)
18 views24 pages

Chapter 5 Engstring Manipulation

The document provides an overview of string manipulation techniques in Python, including string creation, traversal, operators, membership, comparison, slicing, and various string functions and methods. It outlines how to create strings, manipulate them using operators, and perform operations like slicing and counting. Additionally, it includes examples and assignments to reinforce learning about string manipulation based on the CBSE curriculum for Class 11.

Uploaded by

mswastik145
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views24 pages

Chapter 5 Engstring Manipulation

The document provides an overview of string manipulation techniques in Python, including string creation, traversal, operators, membership, comparison, slicing, and various string functions and methods. It outlines how to create strings, manipulate them using operators, and perform operations like slicing and counting. Additionally, it includes examples and assignments to reinforce learning about string manipulation based on the CBSE curriculum for Class 11.

Uploaded by

mswastik145
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

String Manipulation

Based on CBSE Curriculum


Class -11

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

Input ( ) always return input in


string form.
Traversal of a string
• Process to access each and every character of a string
for the purpose of display or for some other purpose is
called string traversal.
Output

Program to print a String after reverse -

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”

»* (it is used to replicate the string)


• like - 5*”@” will result into “@@@@@”
• Like - “go!” * 3 will result “go!go!go!”

note : - “5” * “6” expression is invalid.


Membership Operators in Strings
• 2 membership operators works with strings are in and not in.
To understand the working of these operators, look at the
given examples -
• in operator results into True or False. Like-
– “a” in “Sanjeev” will result into True.
– “ap” in “Sanjeev” will result into False.
– “anj” in “Sanjeev” will result into True.
• not in operator also results into True or False. Like-
– “k” not in “Sanjeev” will result into True.
– “ap” not in “Sanjeev” will result into True.
– “anj” not in “Sanjeev” will result into False.

Neha Tyagi, KV 5 Jaipur II Shift


String Comparison Operators
• Look carefully at following examples -
• “a” == “a” True
• “abc”==“abc” True
• “a”!=“abc” True
• “A”==“a” Fals
• “abc” e
==“Abc” Fals
• ‘a’<‘A’ e
False

How to get Ordinal/Unicode Values?


(beca
use
Unico
• Look at following examples-
de
>>>ord (‘A’) value >>>char(97
of
65 lower ) a
case
>>>ord(‘a’) is >>>char(65)
higher
97 than
A
upper
case)
String Slicing
• Look at following examples carefully-
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13

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!'

>>> str1.title() '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

>>> str1 = 'Hello World! Hello Hello'


>>> str1.count('Hello',12,25) 2
>>> str1.count('Hello’) 3
STRING FUNCTIONS AND METHODS
find(str,start, end)

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 = 'Hello World! Hello Hello'

>>> 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

>>> str1 = 'Hello World! Hello Hello'


>>> str1.index('Hello')
0
>>> str1.index('Hee')
ValueError: substring not found
endswith()
Returns True if the given string ends with the supplied substring
otherwise returns False
>>> str1 = 'Hello World!'
>>> str1.endswith('World!') True
>>> str1.endswith('!') True
>>> str1.endswith('lde') False
startswith()
Returns True if the given string starts with the supplied substring
otherwise returns False
>>> str1 = 'Hello World!'
>>> str1.startswith('He') True
>>> str1.startswith('Hee') False
String Functions
String.capitalize() Converts first character to Capital Letter

String.find() Returns the Lowest Index of Substring

String.index() Returns Index of Substring

String.isalnum() Checks Alphanumeric Character

String.isalpha() Checks if All Characters are Alphabets

String.isdigit() Checks Digit Characters

String.islower() Checks if all Alphabets in a String.are Lowercase

String.isupper() returns if all characters are uppercase characters

String.join() Returns a Concatenated String

String.lower() returns lowercased string

String.upper() returns uppercased string

len() Returns Length of an Object

ord() returns Unicode code point for Unicode character

reversed() returns reversed iterator of a sequence

slice() creates a sliceNeha


object specified
Tyagi, byIIrange()
KV 5 Jaipur Shift
GIVE THE OUTPUT
string=input(“Enter a string”)
while len(string)<=4:
if string[-1]==‘z’:
string=string[0:3]+’c’
elif ‘a’ in string:
string=String[0]+’bb’
elif not int(string[0]):
string=‘1’+string[1:]+’z’
else:
string=string+’*’
print(string)

1) 1bzz 2) 1a 3) abc 4) 0xy 5)xyz


OUTPUT
1bzc*
GIVE THE OUTPUT

string=input(“Enter a string”)
count=3

while True: 1) aabbcc


bbcc
if string[0]==‘a’:
string=string[2:]
elif string[-1]==‘b’:

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.

3. WAP to find the length of a string.

Neha Tyagi, KV 5 Jaipur II Shift


Thank
you
Please follow us on our blog

www.pythontrends.wordpress.com

Neha Tyagi, KV 5 Jaipur II Shift

You might also like