SlideShare a Scribd company logo
2
Most read
4
Most read
13
Most read
STRING MANIPULATION IN PYTHON
PROF POOJAB S
 Getting Length of a string
 Traversal through a string with a loop
 String slices
 Strings are immutable
 Looping and Counting
 The in operator
String Manipulation in Python
 String is a sequence of characters.
 Enclosed within pair of single/double quotes.
 Character has an index Number.
 Eg: “Hello World”
 Access characters using Index.
 Eg: word1='Hello'
word2='Hi'
x=word1[1]
print(x)
y=word2[0]
print(y)
Character H e l l o W o r l D
Index 0 1 2 3 4 5 6 7 8 9 10
 End of the string can be accessed using Negative Index.
 Eg: “Hello World”
 Eg: word=“Hello”
x=word[-1]
print(x)
x=word[-5]
print(x)
Character H e l l o W o r l
Negative Index -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
d
Getting Length of a String using len()
 Built-in function
 Obtain length of a string.
 Eg: word="Python"
l=len(word)
print(l)
Traversal through String with a loop
 Extracting every character and performing some action is called Traversal.
 It can be done using while or for loop
1. Using for loop
s="Python"
for i in s:
print(i, end=‘t’)
2. Using while loop
s="Python"
i=0
while i<len(s): #FORWARD TRAVERSAL
print(s[i],end='t')
i=i+1
s="Python"
i=-1
while i>= -len(s): #BACKWARD TRAVERSAL
print(s[i],end='t')
i=i-1
String Slices
 Portion of a string is a string slice.
 Extract a required number of characters using colon (:)
 Syntax:
st[i:j:k]
 i is the first index or beginning index or start index
 j is the last index or end index. If j index is not present, means slice should be till the end of the string.
 k is the stride, to indicate no. of steps incremented. Default value is 1.
 Eg:
st=“Hello World”
print(st[0:5:1])
1. print (st[0:5])
2. print (st[3:8])
3. print (st[7:])
4. print (st[::])
5. print (st[::2])
6. print (st[4:4])
7. print (st[3:8:2])
8. print (st[1:8:3])
9. print (st[-4:-1])
10. print (st[-1])
11. print (st[:-1])
12. print (st[:])
13. print (st[::-1])
14. print (st[::-2])
 Strings are immutable [cannot change]
 To modify string, create a new string.
 Eg:
1. s="hello world“ #TypeError: 'str' object does not support item assignment
s[3]='t‘
2. s="hello world"
s1=s[:3]+'t'+s[4:] #helto world
print(s1)
String are immutable
 Using loops we can count the frequency of character.
 Eg:
w='Book'
count=0
for letter in w:
if letter=='o':
count=count+1
print("The Occurences of Character 'o' is %d"%(count))
Looping and Counting
 Boolean operator takes 2 string operands.
 Returns True if 1st operand appears in 2nd Operand. Else False.
 Eg:
‘el’ in ‘hello’ #True
‘x’ in ‘hello’ #False
‘EL’ in ‘Hello’ #False
The in operator
 Comparison operators like: <,> and == applied to string objects.
 Result in True or False
 Comparisons happens using ACSII codes
 Eg:
1. s='hello'
if s=='hello':
print('Same') #Same
2. s='hello'
if s<='Hello':
print('Lesser')
else:
print('Greater') #Greater
String Comparison
 A-Z 65-90
 a-z 97-122
 0-9 48-57
 Space 32
 Enter 13

More Related Content

What's hot (20)

PPTX
Python dictionary
Sagar Kumar
 
PPTX
Data Structures in Python
Devashish Kumar
 
PDF
Python Flow Control
Mohammed Sikander
 
PPTX
List in Python
Siddique Ibrahim
 
PPTX
Functions in python slide share
Devashish Kumar
 
PDF
Python list
Mohammed Sikander
 
PPTX
Python Functions
Mohammed Sikander
 
PPT
Array in c
Ravi Gelani
 
PPTX
Chapter 14 strings
Praveen M Jigajinni
 
PDF
Python tuple
Mohammed Sikander
 
PPTX
Python basics
RANAALIMAJEEDRAJPUT
 
PPTX
Functions in Python
Kamal Acharya
 
PDF
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
PPTX
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
PPTX
File handling in Python
Megha V
 
PPTX
Looping statement in python
RaginiJain21
 
PPTX
Modules in Python Programming
sambitmandal
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
Tuple in python
Sharath Ankrajegowda
 
Python dictionary
Sagar Kumar
 
Data Structures in Python
Devashish Kumar
 
Python Flow Control
Mohammed Sikander
 
List in Python
Siddique Ibrahim
 
Functions in python slide share
Devashish Kumar
 
Python list
Mohammed Sikander
 
Python Functions
Mohammed Sikander
 
Array in c
Ravi Gelani
 
Chapter 14 strings
Praveen M Jigajinni
 
Python tuple
Mohammed Sikander
 
Python basics
RANAALIMAJEEDRAJPUT
 
Functions in Python
Kamal Acharya
 
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
File handling in Python
Megha V
 
Looping statement in python
RaginiJain21
 
Modules in Python Programming
sambitmandal
 
Python Modules
Nitin Reddy Katkam
 
Tuple in python
Sharath Ankrajegowda
 

Similar to String Manipulation in Python (20)

PPTX
Strings.pptx
Yagna15
 
PDF
Strings brief introduction in python.pdf
TODAYIREAD1
 
PPTX
Python Strings and its Featues Explained in Detail .pptx
parmg0960
 
PPTX
Lexture about strings, all examples and theoretical part is included
abzalbekulasbekov
 
PPTX
Python String Revisited.pptx
Chandrapriya Jayabal
 
PPTX
06-Strings.pptx
RaviAr5
 
PDF
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
PDF
Python data handling
Prof. Dr. K. Adisesha
 
PPTX
DATA TYPES IN PYTHON jesjdjdjkdkkdk.pptx
rajvishnuf9
 
PPTX
Introduction to python programming ( part-3 )
Ziyauddin Shaik
 
PDF
00012.PYTHON - STRING MANIPULATION SLIDES
PanavGupta3
 
PPTX
Engineering string(681) concept ppt.pptx
ChandrashekarReddy98
 
PDF
Python- strings
Krishna Nanda
 
PDF
Strings in Python
nitamhaske
 
PDF
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
PPTX
Python Programming | JNTUA | UNIT 3 | Strings |
FabMinds
 
PPTX
"Strings in Python - Presentation Slide"
altacc1921
 
PPTX
Chapter 11 Strings and methods [Autosaved].pptx
geethar79
 
PPTX
string manipulation in python ppt for grade 11 cbse
KrithikaTM
 
PPTX
Python Programming-UNIT-II - Strings.pptx
KavithaDonepudi
 
Strings.pptx
Yagna15
 
Strings brief introduction in python.pdf
TODAYIREAD1
 
Python Strings and its Featues Explained in Detail .pptx
parmg0960
 
Lexture about strings, all examples and theoretical part is included
abzalbekulasbekov
 
Python String Revisited.pptx
Chandrapriya Jayabal
 
06-Strings.pptx
RaviAr5
 
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
S.Mohideen Badhusha
 
Python data handling
Prof. Dr. K. Adisesha
 
DATA TYPES IN PYTHON jesjdjdjkdkkdk.pptx
rajvishnuf9
 
Introduction to python programming ( part-3 )
Ziyauddin Shaik
 
00012.PYTHON - STRING MANIPULATION SLIDES
PanavGupta3
 
Engineering string(681) concept ppt.pptx
ChandrashekarReddy98
 
Python- strings
Krishna Nanda
 
Strings in Python
nitamhaske
 
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Python Programming | JNTUA | UNIT 3 | Strings |
FabMinds
 
"Strings in Python - Presentation Slide"
altacc1921
 
Chapter 11 Strings and methods [Autosaved].pptx
geethar79
 
string manipulation in python ppt for grade 11 cbse
KrithikaTM
 
Python Programming-UNIT-II - Strings.pptx
KavithaDonepudi
 
Ad

More from Pooja B S (6)

PPTX
Iteration
Pooja B S
 
PPTX
String Methods and Files
Pooja B S
 
PPTX
Lists in Python
Pooja B S
 
PPTX
Dictionary
Pooja B S
 
PPTX
Python Conditionals and Functions
Pooja B S
 
PPTX
Python Basics
Pooja B S
 
Iteration
Pooja B S
 
String Methods and Files
Pooja B S
 
Lists in Python
Pooja B S
 
Dictionary
Pooja B S
 
Python Conditionals and Functions
Pooja B S
 
Python Basics
Pooja B S
 
Ad

Recently uploaded (20)

PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Difference between write and update in odoo 18
Celine George
 
epi editorial commitee meeting presentation
MIPLM
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Introduction presentation of the patentbutler tool
MIPLM
 
infertility, types,causes, impact, and management
Ritu480198
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 

String Manipulation in Python

  • 1. STRING MANIPULATION IN PYTHON PROF POOJAB S
  • 2.  Getting Length of a string  Traversal through a string with a loop  String slices  Strings are immutable  Looping and Counting  The in operator String Manipulation in Python
  • 3.  String is a sequence of characters.  Enclosed within pair of single/double quotes.  Character has an index Number.  Eg: “Hello World”  Access characters using Index.  Eg: word1='Hello' word2='Hi' x=word1[1] print(x) y=word2[0] print(y) Character H e l l o W o r l D Index 0 1 2 3 4 5 6 7 8 9 10
  • 4.  End of the string can be accessed using Negative Index.  Eg: “Hello World”  Eg: word=“Hello” x=word[-1] print(x) x=word[-5] print(x) Character H e l l o W o r l Negative Index -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 d
  • 5. Getting Length of a String using len()  Built-in function  Obtain length of a string.  Eg: word="Python" l=len(word) print(l) Traversal through String with a loop  Extracting every character and performing some action is called Traversal.  It can be done using while or for loop
  • 6. 1. Using for loop s="Python" for i in s: print(i, end=‘t’) 2. Using while loop s="Python" i=0 while i<len(s): #FORWARD TRAVERSAL print(s[i],end='t') i=i+1 s="Python" i=-1 while i>= -len(s): #BACKWARD TRAVERSAL print(s[i],end='t') i=i-1
  • 7. String Slices  Portion of a string is a string slice.  Extract a required number of characters using colon (:)  Syntax: st[i:j:k]  i is the first index or beginning index or start index  j is the last index or end index. If j index is not present, means slice should be till the end of the string.  k is the stride, to indicate no. of steps incremented. Default value is 1.  Eg: st=“Hello World” print(st[0:5:1])
  • 8. 1. print (st[0:5]) 2. print (st[3:8]) 3. print (st[7:]) 4. print (st[::]) 5. print (st[::2]) 6. print (st[4:4]) 7. print (st[3:8:2]) 8. print (st[1:8:3]) 9. print (st[-4:-1]) 10. print (st[-1]) 11. print (st[:-1]) 12. print (st[:]) 13. print (st[::-1]) 14. print (st[::-2])
  • 9.  Strings are immutable [cannot change]  To modify string, create a new string.  Eg: 1. s="hello world“ #TypeError: 'str' object does not support item assignment s[3]='t‘ 2. s="hello world" s1=s[:3]+'t'+s[4:] #helto world print(s1) String are immutable
  • 10.  Using loops we can count the frequency of character.  Eg: w='Book' count=0 for letter in w: if letter=='o': count=count+1 print("The Occurences of Character 'o' is %d"%(count)) Looping and Counting
  • 11.  Boolean operator takes 2 string operands.  Returns True if 1st operand appears in 2nd Operand. Else False.  Eg: ‘el’ in ‘hello’ #True ‘x’ in ‘hello’ #False ‘EL’ in ‘Hello’ #False The in operator
  • 12.  Comparison operators like: <,> and == applied to string objects.  Result in True or False  Comparisons happens using ACSII codes  Eg: 1. s='hello' if s=='hello': print('Same') #Same 2. s='hello' if s<='Hello': print('Lesser') else: print('Greater') #Greater String Comparison
  • 13.  A-Z 65-90  a-z 97-122  0-9 48-57  Space 32  Enter 13