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

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

Lists in Python
Lists in PythonLists in Python
Lists in PythonPooja B S
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in PythonPooja B S
 
Python Conditionals and Functions
Python Conditionals and FunctionsPython Conditionals and Functions
Python Conditionals and FunctionsPooja B S
 
Python Basics
Python BasicsPython Basics
Python BasicsPooja 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

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 

Recently uploaded (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

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