SlideShare a Scribd company logo
2
Most read
5
Most read
9
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

PPTX
Python strings presentation
PPTX
Error and exception in python
PPTX
List in Python
PDF
Strings in Python
PPTX
Python Datatypes by SujithKumar
PPTX
Chapter 14 strings
PPTX
Chapter 9 python fundamentals
Python strings presentation
Error and exception in python
List in Python
Strings in Python
Python Datatypes by SujithKumar
Chapter 14 strings
Chapter 9 python fundamentals

What's hot (20)

PPTX
Chapter 05 classes and objects
PPTX
Python basics
PDF
Datatypes in python
PDF
Strings in python
PPTX
Python for loop
PDF
Python tuple
PPTX
Python Functions
PDF
Python strings
PPTX
File Handling Python
PPT
Introduction to Python
PDF
C++ OOPS Concept
PPTX
Chapter 10 data handling
PDF
Python list
PPTX
Strings in c++
ODP
Python Modules
PPTX
Strings in Python
PDF
Python programming : Strings
PPS
Wrapper class
PPTX
Looping statement in python
PDF
Python Flow Control
Chapter 05 classes and objects
Python basics
Datatypes in python
Strings in python
Python for loop
Python tuple
Python Functions
Python strings
File Handling Python
Introduction to Python
C++ OOPS Concept
Chapter 10 data handling
Python list
Strings in c++
Python Modules
Strings in Python
Python programming : Strings
Wrapper class
Looping statement in python
Python Flow Control
Ad

Similar to String Manipulation in Python (20)

PPTX
Strings.pptx
PDF
Strings brief introduction in python.pdf
PPTX
Python Strings and its Featues Explained in Detail .pptx
PPTX
Lexture about strings, all examples and theoretical part is included
PPTX
Python String Revisited.pptx
PPTX
06-Strings.pptx
PDF
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
PDF
Python data handling
PPTX
DATA TYPES IN PYTHON jesjdjdjkdkkdk.pptx
PDF
14-Strings-In-Python strings with oops .pdf
PPTX
Introduction to python programming ( part-3 )
PDF
00012.PYTHON - STRING MANIPULATION SLIDES
PPTX
Engineering string(681) concept ppt.pptx
PDF
Python- strings
PPTX
Python Programming | JNTUA | UNIT 3 | Strings |
PPTX
"Strings in Python - Presentation Slide"
PPTX
Chapter 11 Strings and methods [Autosaved].pptx
PPTX
string manipulation in python ppt for grade 11 cbse
PPTX
Python Programming-UNIT-II - Strings.pptx
PPTX
"Strings in Python - Presentation Slide"
Strings.pptx
Strings brief introduction in python.pdf
Python Strings and its Featues Explained in Detail .pptx
Lexture about strings, all examples and theoretical part is included
Python String Revisited.pptx
06-Strings.pptx
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
Python data handling
DATA TYPES IN PYTHON jesjdjdjkdkkdk.pptx
14-Strings-In-Python strings with oops .pdf
Introduction to python programming ( part-3 )
00012.PYTHON - STRING MANIPULATION SLIDES
Engineering string(681) concept ppt.pptx
Python- strings
Python Programming | JNTUA | UNIT 3 | Strings |
"Strings in Python - Presentation Slide"
Chapter 11 Strings and methods [Autosaved].pptx
string manipulation in python ppt for grade 11 cbse
Python Programming-UNIT-II - Strings.pptx
"Strings in Python - Presentation Slide"
Ad

More from Pooja B S (6)

PPTX
Iteration
PPTX
String Methods and Files
PPTX
Lists in Python
PPTX
Dictionary
PPTX
Python Conditionals and Functions
PPTX
Python Basics
Iteration
String Methods and Files
Lists in Python
Dictionary
Python Conditionals and Functions
Python Basics

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Pre independence Education in Inndia.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
VCE English Exam - Section C Student Revision Booklet
Pre independence Education in Inndia.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Insiders guide to clinical Medicine.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPH.pptx obstetrics and gynecology in nursing
STATICS OF THE RIGID BODIES Hibbelers.pdf
Complications of Minimal Access Surgery at WLH
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
102 student loan defaulters named and shamed – Is someone you know on the list?
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Week 4 Term 3 Study Techniques revisited.pptx
O7-L3 Supply Chain Operations - ICLT Program

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