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)

File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Strings
StringsStrings
Strings
 
Python - Lecture 11
Python - Lecture 11Python - Lecture 11
Python - Lecture 11
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Python list
Python listPython list
Python list
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
 

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
 
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
 
Python basics
Python basicsPython basics
Python basics
 

Recently uploaded

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
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
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
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
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
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
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
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 🔝✔️✔️
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 

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