SlideShare a Scribd company logo
1 of 13
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)

Operators in python
Operators in pythonOperators in python
Operators in python
 
Python basics
Python basicsPython basics
Python basics
 
Python basic
Python basicPython basic
Python basic
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
List in Python
List in PythonList in Python
List in Python
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
String in java
String in javaString in java
String in java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Function in c
Function in cFunction in c
Function in c
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Strings
StringsStrings
Strings
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Sorting
SortingSorting
Sorting
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 

Similar to String Manipulation in Python

Similar to String Manipulation in Python (20)

Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )
 
Sixth session
Sixth sessionSixth session
Sixth session
 
STRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfSTRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdf
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Core Concept_Python.pptx
Core Concept_Python.pptxCore Concept_Python.pptx
Core Concept_Python.pptx
 
Python Basics
Python Basics Python Basics
Python Basics
 
Basics of python 3
Basics of python 3Basics of python 3
Basics of python 3
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)
 
Python slide.1
Python slide.1Python slide.1
Python slide.1
 
Team 1
Team 1Team 1
Team 1
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
 
Introduction To Python
Introduction To  PythonIntroduction To  Python
Introduction To Python
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 
Pythonppt28 11-18
Pythonppt28 11-18Pythonppt28 11-18
Pythonppt28 11-18
 
FUNDAMENTALS OF PYTHON LANGUAGE
 FUNDAMENTALS OF PYTHON LANGUAGE  FUNDAMENTALS OF PYTHON LANGUAGE
FUNDAMENTALS OF PYTHON LANGUAGE
 
Python ds
Python dsPython ds
Python ds
 
Python- strings
Python- stringsPython- strings
Python- strings
 
Python ppt
Python pptPython ppt
Python ppt
 
Iteration
IterationIteration
Iteration
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 

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