SlideShare a Scribd company logo
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 python
Celine George
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
Pooja B S
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
hydpy
 
Python functions part11
Python functions  part11Python functions  part11
Python functions part11
Vishal 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 181
Mahmoud Samir Fayed
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming Language
Rohan 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 84
Mahmoud Samir Fayed
 
Baabtra.com little coder chapter - 7
Baabtra.com little coder   chapter - 7Baabtra.com little coder   chapter - 7
Baabtra.com little coder chapter - 7
baabtra.com - No. 1 supplier of quality freshers
 
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
Mahmoud 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 84
Mahmoud 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 84
Mahmoud 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
 
Groovy
GroovyGroovy
Groovy
Vijay Shukla
 
Python Assignment 1 Answers (1).docx
Python Assignment 1 Answers (1).docxPython Assignment 1 Answers (1).docx
Python Assignment 1 Answers (1).docx
ShubhamTripathi290909
 
Python for loop
Python for loopPython for loop
Python for loop
Aishwarya Deshmukh
 
Data structures Lecture 5
Data structures Lecture 5Data structures Lecture 5
Data structures Lecture 5
AzharIqbal710687
 
Slicing in Python - What is It?
Slicing in Python - What is It?Slicing in Python - What is It?
Slicing in Python - What is It?
💾 Radek Fabisiak
 

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

Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
Panimalar Engineering College
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
Mr. Vikram Singh Slathia
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
MihirDatir
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
AnirudhaGaikwad4
 
Python - Data Collection
Python - Data CollectionPython - Data Collection
Python - Data Collection
JoseTanJr
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slides
aiclub_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
AUNGHTET61
 
Module-2.pptx
Module-2.pptxModule-2.pptx
Module-2.pptx
GaganRaj28
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
ssuser8e50d8
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, Exceptions
Sreedhar Chowdam
 
cover every basics of python with this..
cover every basics of python with this..cover every basics of python with this..
cover every basics of python with this..
karkimanish411
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptx
DEEPAK948083
 
Python data type
Python data typePython data type
Python data type
Jaya Kumari
 
Chapter05.ppt
Chapter05.pptChapter05.ppt
Chapter05.ppt
afsheenfaiq2
 
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
ElNew2
 
Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat Sheet
Verxus
 
2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf
MeghanaDBengalur
 
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
TURAGAVIJAYAAKASH
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
H2Kinfosys
 

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
 
cover every basics of python with this..
cover every basics of python with this..cover every basics of python with this..
cover every basics of python with this..
 
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
 
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
 
Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat Sheet
 
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
 
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
 

More from Munazza-Mah-Jabeen

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

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

MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 

Recently uploaded (20)

MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 

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)