SlideShare a Scribd company logo
1 of 15
8.1 Sequences
8-1
Concept:
A sequence is an object that holds
multiple items of data, stored one after
the other. You can perform operations on
a sequence, to examine and manipulate
the items stored in it.
8.2 Working with Strings
8-2
Concept:
Python provides several ways to access
the individual characters in a string.
Strings also have methods that allow you
to perform operations on them.
8.2 Working with Strings
8-3
Accessing the Individual Characters in a String
Iterating Over a String with the for Loop
Program 8-1
(count_Ts.py)
8.2 Working with Strings
8-4
Accessing the Individual Characters in a String
Indexing
my_string = ‘Roses are red’
print my_string[0], my_string[6], my_string[10]
print my_string[-1], my_string[-2], my_string[-3]
Figure 8-2 String indexes
8.2 Working with Strings
8-5
Accessing the Individual Characters in a String
IndexError Exceptions
IndexError exception will if an out of range index
is accessed
For example:
city = ‘Boston’
index = 0
while index < 7:
print city[index]
index += 1
B o s t o n
     
0 1 2 3 4 5
8.2 Working with Strings
8-6
Accessing the Individual Characters in a String
The len Function
len returns the length of a sequence
For example:
city = ‘Boston’
index = 0
while index < len(city):
print city[index]
index += 1
B o s t o n
     
0 1 2 3 4 5
8.2 Working with Strings
8-7
Strings Are Immutable
Program 8-2 (concatenate.py)
Figure 8-4 The string
'Carmen' assigned to name
Figure 8-5 The string
'Carmen Brown' assigned
to name
8.2 Working with Strings
8-8
Strings Are Immutable
Immutable means that once they are created they
cannot be changed.
For example:
friend = ‘Bill’
friend[0] = ‘J’ # No, this will cause an error!
B i l l
   
0 1 2 3
8.2 Working with Strings
8-9
String Slicing
Slice is a span of items that are taken from a
sequence.
String slices are also called substrings.
string[start : end]
8.2 Working with Strings
8-10
String Slicing
full_name = ‘Patty Lynn Smith’
middle_name = full_name[6:10] # middle_name <= ‘Lynn ‘
first_name = full_name[:5] # first_name <= ‘Patty ‘
last_name = full_name[11:] # last_name <= ‘Smith’
my_string = full_name[:] # my_string <= ‘Patty Lynn Smith’
my_string = full_name[0:len(full_name)] # my_string <= ‘Patty Lynn Smith’
last_name = full_name[-5:] # last_name <= ‘Smith’
P a t t y L y n n S m i t h
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-16 - 15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 - 2 -1
8.2 Working with Strings
8-11
String Slicing
string[start : end : step]
letters = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’
print letters[0:26:2] # ‘ACEGIKMOQSUWY’
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
8.2 Working with Strings
8-12
Testing Strings with in and not in
string1 in string2
in operator determines whether one string is contained in
another string
text = ‘Four score and seven years ago’
if ‘seven’ in text:
print ‘The string “seven” was found.’
else:
print ‘The string “seven” was not found.’
string1 in string2
not in operator determines whether one string is
contained in another string
name = ‘Bill Joanne Susan Chris Juan Katie’
if ‘Pierre’ not in text:
print ‘Pierre was not found.’
else:
print ‘Pierre was found.’
8.2 Working with Strings
8-13
String Methods
String Testing Methods
Table 8-1 Some string testing methods
8.2 Working with Strings
8-14
String Methods
Modification Methods
Table 8-2 String Modification Methods
8.2 Working with Strings
8-15
The Repetition Operator … *
Program 8-8 (repetition_operator.py)

More Related Content

What's hot

Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonCeline George
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in PythonPooja B S
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
Python functions part11
Python functions  part11Python functions  part11
Python functions part11Vishal Dutt
 
The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181Mahmoud Samir Fayed
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming LanguageRohan Gupta
 
The Ring programming language version 1.2 book - Part 12 of 84
The Ring programming language version 1.2 book - Part 12 of 84The Ring programming language version 1.2 book - Part 12 of 84
The Ring programming language version 1.2 book - Part 12 of 84Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 9 of 84
The Ring programming language version 1.2 book - Part 9 of 84The Ring programming language version 1.2 book - Part 9 of 84
The Ring programming language version 1.2 book - Part 9 of 84Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.2 book - Part 10 of 84The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.2 book - Part 10 of 84Mahmoud Samir Fayed
 
Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Rupak Roy
 
Merging tables using R
Merging tables using R Merging tables using R
Merging tables using R Rupak Roy
 
Python Assignment 1 Answers (1).docx
Python Assignment 1 Answers (1).docxPython Assignment 1 Answers (1).docx
Python Assignment 1 Answers (1).docxShubhamTripathi290909
 

What's hot (20)

Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Dictionaries in Python
Dictionaries in PythonDictionaries in Python
Dictionaries in Python
 
Python functions part11
Python functions  part11Python functions  part11
Python functions part11
 
The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming Language
 
The Ring programming language version 1.2 book - Part 12 of 84
The Ring programming language version 1.2 book - Part 12 of 84The Ring programming language version 1.2 book - Part 12 of 84
The Ring programming language version 1.2 book - Part 12 of 84
 
Baabtra.com little coder chapter - 7
Baabtra.com little coder   chapter - 7Baabtra.com little coder   chapter - 7
Baabtra.com little coder chapter - 7
 
The Ring programming language version 1.2 book - Part 9 of 84
The Ring programming language version 1.2 book - Part 9 of 84The Ring programming language version 1.2 book - Part 9 of 84
The Ring programming language version 1.2 book - Part 9 of 84
 
The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 11 of 84
 
The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.2 book - Part 10 of 84The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.2 book - Part 10 of 84
 
Apache Pig Relational Operators - II
Apache Pig Relational Operators - II Apache Pig Relational Operators - II
Apache Pig Relational Operators - II
 
Merging tables using R
Merging tables using R Merging tables using R
Merging tables using R
 
Groovy
GroovyGroovy
Groovy
 
Python Assignment 1 Answers (1).docx
Python Assignment 1 Answers (1).docxPython Assignment 1 Answers (1).docx
Python Assignment 1 Answers (1).docx
 
Python for loop
Python for loopPython for loop
Python for loop
 
Data structures Lecture 5
Data structures Lecture 5Data structures Lecture 5
Data structures Lecture 5
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
Slicing in Python - What is It?
Slicing in Python - What is It?Slicing in Python - What is It?
Slicing in Python - What is It?
 

Similar to More About Strings

Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxMihirDatir
 
Python - Data Collection
Python - Data CollectionPython - Data Collection
Python - Data CollectionJoseTanJr
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slidesaiclub_slides
 
Python elements list you can study .pdf
Python elements list you can study  .pdfPython elements list you can study  .pdf
Python elements list you can study .pdfAUNGHTET61
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptxssuser8e50d8
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsSreedhar Chowdam
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptxDEEPAK948083
 
Python data type
Python data typePython data type
Python data typeJaya Kumari
 
python cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningpython cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningTURAGAVIJAYAAKASH
 
Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat SheetVerxus
 
beginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdfbeginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdfElNew2
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And AnswersH2Kinfosys
 

Similar to More About Strings (20)

Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
 
Python - Data Collection
Python - Data CollectionPython - Data Collection
Python - Data Collection
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slides
 
Python elements list you can study .pdf
Python elements list you can study  .pdfPython elements list you can study  .pdf
Python elements list you can study .pdf
 
Module-2.pptx
Module-2.pptxModule-2.pptx
Module-2.pptx
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, Exceptions
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptx
 
Python data type
Python data typePython data type
Python data type
 
Chapter05.ppt
Chapter05.pptChapter05.ppt
Chapter05.ppt
 
2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf
 
python cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningpython cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learning
 
Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat Sheet
 
beginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdfbeginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdf
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
 
Chapter05
Chapter05Chapter05
Chapter05
 

More from Munazza-Mah-Jabeen (20)

Virtual Functions
Virtual FunctionsVirtual Functions
Virtual Functions
 
The Standard Template Library
The Standard Template LibraryThe Standard Template Library
The Standard Template Library
 
Object-Oriented Software
Object-Oriented SoftwareObject-Oriented Software
Object-Oriented Software
 
Templates and Exceptions
 Templates and Exceptions Templates and Exceptions
Templates and Exceptions
 
Dictionaries and Sets
Dictionaries and SetsDictionaries and Sets
Dictionaries and Sets
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
 
Lists and Tuples
Lists and TuplesLists and Tuples
Lists and Tuples
 
Files and Exceptions
Files and ExceptionsFiles and Exceptions
Files and Exceptions
 
Functions
FunctionsFunctions
Functions
 
Pointers
PointersPointers
Pointers
 
Repitition Structure
Repitition StructureRepitition Structure
Repitition Structure
 
Inheritance
InheritanceInheritance
Inheritance
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Arrays and Strings
Arrays and StringsArrays and Strings
Arrays and Strings
 
Objects and Classes
Objects and ClassesObjects and Classes
Objects and Classes
 
Functions
FunctionsFunctions
Functions
 
Structures
StructuresStructures
Structures
 
Loops and Decisions
Loops and DecisionsLoops and Decisions
Loops and Decisions
 
C++ programming basics
C++ programming basicsC++ programming basics
C++ programming basics
 

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
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
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
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
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
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
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
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
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
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 

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.
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
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
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
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
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE 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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
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
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
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
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 

More About Strings

  • 1. 8.1 Sequences 8-1 Concept: A sequence is an object that holds multiple items of data, stored one after the other. You can perform operations on a sequence, to examine and manipulate the items stored in it.
  • 2. 8.2 Working with Strings 8-2 Concept: Python provides several ways to access the individual characters in a string. Strings also have methods that allow you to perform operations on them.
  • 3. 8.2 Working with Strings 8-3 Accessing the Individual Characters in a String Iterating Over a String with the for Loop Program 8-1 (count_Ts.py)
  • 4. 8.2 Working with Strings 8-4 Accessing the Individual Characters in a String Indexing my_string = ‘Roses are red’ print my_string[0], my_string[6], my_string[10] print my_string[-1], my_string[-2], my_string[-3] Figure 8-2 String indexes
  • 5. 8.2 Working with Strings 8-5 Accessing the Individual Characters in a String IndexError Exceptions IndexError exception will if an out of range index is accessed For example: city = ‘Boston’ index = 0 while index < 7: print city[index] index += 1 B o s t o n       0 1 2 3 4 5
  • 6. 8.2 Working with Strings 8-6 Accessing the Individual Characters in a String The len Function len returns the length of a sequence For example: city = ‘Boston’ index = 0 while index < len(city): print city[index] index += 1 B o s t o n       0 1 2 3 4 5
  • 7. 8.2 Working with Strings 8-7 Strings Are Immutable Program 8-2 (concatenate.py) Figure 8-4 The string 'Carmen' assigned to name Figure 8-5 The string 'Carmen Brown' assigned to name
  • 8. 8.2 Working with Strings 8-8 Strings Are Immutable Immutable means that once they are created they cannot be changed. For example: friend = ‘Bill’ friend[0] = ‘J’ # No, this will cause an error! B i l l     0 1 2 3
  • 9. 8.2 Working with Strings 8-9 String Slicing Slice is a span of items that are taken from a sequence. String slices are also called substrings. string[start : end]
  • 10. 8.2 Working with Strings 8-10 String Slicing full_name = ‘Patty Lynn Smith’ middle_name = full_name[6:10] # middle_name <= ‘Lynn ‘ first_name = full_name[:5] # first_name <= ‘Patty ‘ last_name = full_name[11:] # last_name <= ‘Smith’ my_string = full_name[:] # my_string <= ‘Patty Lynn Smith’ my_string = full_name[0:len(full_name)] # my_string <= ‘Patty Lynn Smith’ last_name = full_name[-5:] # last_name <= ‘Smith’ P a t t y L y n n S m i t h 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -16 - 15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 - 2 -1
  • 11. 8.2 Working with Strings 8-11 String Slicing string[start : end : step] letters = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’ print letters[0:26:2] # ‘ACEGIKMOQSUWY’ ABCDEFGHIJKLMNOPQRSTUVWXYZ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  • 12. 8.2 Working with Strings 8-12 Testing Strings with in and not in string1 in string2 in operator determines whether one string is contained in another string text = ‘Four score and seven years ago’ if ‘seven’ in text: print ‘The string “seven” was found.’ else: print ‘The string “seven” was not found.’ string1 in string2 not in operator determines whether one string is contained in another string name = ‘Bill Joanne Susan Chris Juan Katie’ if ‘Pierre’ not in text: print ‘Pierre was not found.’ else: print ‘Pierre was found.’
  • 13. 8.2 Working with Strings 8-13 String Methods String Testing Methods Table 8-1 Some string testing methods
  • 14. 8.2 Working with Strings 8-14 String Methods Modification Methods Table 8-2 String Modification Methods
  • 15. 8.2 Working with Strings 8-15 The Repetition Operator … * Program 8-8 (repetition_operator.py)