SlideShare a Scribd company logo
STRING METHODS AND FILES
PROF POOJAB S
 String is a class
 An object of the string class will be created
 Class is a collection of member variables and member functions
 Python provides built-in set of member classes.
 Accessed using dot operator
objName.memberMethod(arguments)
 dir provides variables and methods of a class
String Methods and Files
 Eg: s=‘Python’
type(s)
dir(s)
 The methods are called using object name: Method Invocation
 Eg: s=‘abc’
s.islower()
s=‘ABC’
s.islower()
 METHODS
1. capitalize()
2. s.upper()
3. s.lower()
4. s.find(s1)
5. s.strip()
6. s.startswith(prefix,start,end)
7. s.count(s1,start,end)
1. capitalize: Returns the capitalize version of the original string
Eg: s='hello'
s1=str.capitalize(s)
print(s1)
s='hello World'
print(s.capitalize())
#Hello world
s='hello World'
print(s.capitalize())
2. s.upper(): Returns a copy of string to uppercase.
Eg: s='hello‘
s1=s.upper()
print(s1)
3. s.lower(): Returns the lower version of string
Eg: s='HELLO'
print(s.lower())
4. s.find(s1): Used to search for a substring. If found, return the index value else return the value as -1.
Eg: s='python'
print(s.find('o')) #4
s='python'
print(s.find('o')) #-1
The find function can take one or more arguments. Start and End
Eg: s='Calender is August'
s.find('Cal')
s.find('cal')
s.find('e',6,10)
5. s.strip(): Returns a copy of string by removing leading (first) and trailing (last) whitespaces.
Eg: s=' Hello World '
s.strip()
s='#####Python#####'
s.strip('#')
s='Hello Python'
s.strip('Hln')
6. s.startswith(prefix,start,end): This function has 3 arguments. The function return True is the s starts with specified
prefix, False otherwise.
Eg: s='hello world'
s.startswith('he')
s='hello world'
s.startswith('w',6)
s='hello world'
s.startswith('wo',6,10)
7. s.count(s1,start,end): Takes 3 arguments. Returns number of occurrence of substring s1 in string s.
Eg: s='Hello how are you? How about you?'
s.count('h')
s.count('H')
s.count('how')
s.count('how',3,10)
Parsing Strings
 Search for substring matching
 Example is email-id of a person.
 We want to extract the domain name [gmail/yahoo/outlook/rediffmail] in email
 Identify the position of @ and identify the whitespace after @
 Extract the substring between @ and whitespace
 Eg: s='From bnmitguest@outlook.com Thursday March 12 2020'
atpos=s.find('@')
print('Position of @',atpos)
spacepos=s.find(' ',atpos)
print('Position of space after @ is',spacepos)
host=s[atpos+1:spacepos]
print(host)
f=open("C:/Users/Pooja B S/Documents/SDP on Python/abc.txt")
>>> print(f.read())
Hello Python Programming
Hello C Programming
Hello C++ Programming
Hello HTML Programming
Hello Perl Programming

More Related Content

Similar to String Methods and Files

Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
varthini python .pptx
varthini python .pptxvarthini python .pptx
varthini python .pptx
MJeyavarthini
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 
String (Computer programming and utilization)
String (Computer programming and utilization)String (Computer programming and utilization)
String (Computer programming and utilization)
Digvijaysinh Gohil
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptx
adityaraj7711
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
adityakumawat625
 
11.ppt
11.ppt11.ppt
strings.ppt
strings.pptstrings.ppt
strings.ppt
BhumaNagaPavan
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper class
SimoniShah6
 
Learn python in 20 minutes
Learn python in 20 minutesLearn python in 20 minutes
Learn python in 20 minutes
Sidharth Nadhan
 
Python data handling
Python data handlingPython data handling
Python data handling
Prof. Dr. K. Adisesha
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
JamesChristianGadian
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
nitamhaske
 
Strings CPU GTU
Strings CPU GTUStrings CPU GTU
Strings CPU GTU
Maharshi Dave
 
beginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxbeginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptx
HongAnhNguyn285885
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulationShahjahan Samoon
 

Similar to String Methods and Files (20)

Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
varthini python .pptx
varthini python .pptxvarthini python .pptx
varthini python .pptx
 
Strings1
Strings1Strings1
Strings1
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
07slide
07slide07slide
07slide
 
String (Computer programming and utilization)
String (Computer programming and utilization)String (Computer programming and utilization)
String (Computer programming and utilization)
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptx
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
 
11.ppt
11.ppt11.ppt
11.ppt
 
strings.ppt
strings.pptstrings.ppt
strings.ppt
 
Team 1
Team 1Team 1
Team 1
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper class
 
Learn python in 20 minutes
Learn python in 20 minutesLearn python in 20 minutes
Learn python in 20 minutes
 
Python data handling
Python data handlingPython data handling
Python data handling
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Strings CPU GTU
Strings CPU GTUStrings CPU GTU
Strings CPU GTU
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
beginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxbeginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptx
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
 

More from Pooja B S

Iteration
IterationIteration
Iteration
Pooja B S
 
Lists in Python
Lists in PythonLists in Python
Lists in Python
Pooja B S
 
Dictionary
DictionaryDictionary
Dictionary
Pooja B S
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
Pooja B S
 
Python Conditionals and Functions
Python Conditionals and FunctionsPython Conditionals and Functions
Python Conditionals and Functions
Pooja B S
 
Python Basics
Python BasicsPython Basics
Python Basics
Pooja B S
 

More from Pooja B S (6)

Iteration
IterationIteration
Iteration
 
Lists in Python
Lists in PythonLists in Python
Lists in Python
 
Dictionary
DictionaryDictionary
Dictionary
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Python Conditionals and Functions
Python Conditionals and FunctionsPython Conditionals and Functions
Python Conditionals and Functions
 
Python Basics
Python BasicsPython Basics
Python Basics
 

Recently uploaded

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
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
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
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
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
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
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
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
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
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)

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
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
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
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
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
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
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
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
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
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
 

String Methods and Files

  • 1. STRING METHODS AND FILES PROF POOJAB S
  • 2.  String is a class  An object of the string class will be created  Class is a collection of member variables and member functions  Python provides built-in set of member classes.  Accessed using dot operator objName.memberMethod(arguments)  dir provides variables and methods of a class String Methods and Files
  • 3.  Eg: s=‘Python’ type(s) dir(s)  The methods are called using object name: Method Invocation  Eg: s=‘abc’ s.islower() s=‘ABC’ s.islower()
  • 4.  METHODS 1. capitalize() 2. s.upper() 3. s.lower() 4. s.find(s1) 5. s.strip() 6. s.startswith(prefix,start,end) 7. s.count(s1,start,end)
  • 5. 1. capitalize: Returns the capitalize version of the original string Eg: s='hello' s1=str.capitalize(s) print(s1) s='hello World' print(s.capitalize()) #Hello world s='hello World' print(s.capitalize()) 2. s.upper(): Returns a copy of string to uppercase. Eg: s='hello‘ s1=s.upper() print(s1)
  • 6. 3. s.lower(): Returns the lower version of string Eg: s='HELLO' print(s.lower()) 4. s.find(s1): Used to search for a substring. If found, return the index value else return the value as -1. Eg: s='python' print(s.find('o')) #4 s='python' print(s.find('o')) #-1 The find function can take one or more arguments. Start and End Eg: s='Calender is August' s.find('Cal') s.find('cal') s.find('e',6,10)
  • 7. 5. s.strip(): Returns a copy of string by removing leading (first) and trailing (last) whitespaces. Eg: s=' Hello World ' s.strip() s='#####Python#####' s.strip('#') s='Hello Python' s.strip('Hln')
  • 8. 6. s.startswith(prefix,start,end): This function has 3 arguments. The function return True is the s starts with specified prefix, False otherwise. Eg: s='hello world' s.startswith('he') s='hello world' s.startswith('w',6) s='hello world' s.startswith('wo',6,10)
  • 9. 7. s.count(s1,start,end): Takes 3 arguments. Returns number of occurrence of substring s1 in string s. Eg: s='Hello how are you? How about you?' s.count('h') s.count('H') s.count('how') s.count('how',3,10)
  • 10. Parsing Strings  Search for substring matching  Example is email-id of a person.  We want to extract the domain name [gmail/yahoo/outlook/rediffmail] in email  Identify the position of @ and identify the whitespace after @  Extract the substring between @ and whitespace  Eg: s='From bnmitguest@outlook.com Thursday March 12 2020' atpos=s.find('@') print('Position of @',atpos) spacepos=s.find(' ',atpos) print('Position of space after @ is',spacepos) host=s[atpos+1:spacepos] print(host)
  • 11. f=open("C:/Users/Pooja B S/Documents/SDP on Python/abc.txt") >>> print(f.read()) Hello Python Programming Hello C Programming Hello C++ Programming Hello HTML Programming Hello Perl Programming