SlideShare a Scribd company logo
http://www.skillbrew.com
/SkillbrewTalent brewed by the
industry itself
Common String Methods
Pavan Verma
Python Programming Essentials
@YinYangPavan
© SkillBrew http://skillbrew.com
Common String Methods
 upper()
 lower()
 capitalize()
 startswith()
 endswith()
 strip()
 find()
 split()
 join()
2
© SkillBrew http://skillbrew.com
upper, lower, capitalize
3
message = "enter the dragon"
print message.upper()
print message.lower()
print message.capitalize()
Outputs:
ENTER THE DRAGON
enter the dragon
Enter the dragon
upper(): Converts all
lowercase letters in string to
uppercase
lower(): Converts all
uppercase letters in string to
lowercase
capitalize():
Capitalizes first letter of
string
© SkillBrew http://skillbrew.com
startswith
4
startswith(text)
• Checks whether the string starts with text
• Return’s True if there is a match otherwise False
>>> str = "explicit is better than implicit"
>>> str.startswith('explicit')
True
>>> str.startswith('Explicit')
False
>>>
© SkillBrew http://skillbrew.com
startswith (2)
5
str.startswith(text, begin, end)
startswith()optionally takes two arguments begin and
end
• text − This is the string to be checked
• begin − This is the optional parameter to set start index of
the matching boundary
• end − This is the optional parameter to set start index of
the matching boundary
© SkillBrew http://skillbrew.com
startswith (3)
6
str = "explicit is better than implicit“
>>> print str.startswith("is", 9)
True
>>> print str.startswith("better", 12, 18)
True
startswith()returns True if matching string found else
False
© SkillBrew http://skillbrew.com
endswith
7
>>> str = "explicit is better than implicit"
>>> str.endswith("implicit")
True
>>> str.endswith("implicit", 20)
True
>>> str.endswith("implicit", 25)
False
>>> str.endswith("than", 19, 23)
True
endswith(text)works the same way as startswith difference
being it checks whether the string ends with text or not
© SkillBrew http://skillbrew.com
message = " enter the dragon "
print message.strip()
Output:
enter the dragon
message = "--enter the dragon--"
print message.strip(‘-’)
Output:
enter the dragon
strip(chars):
• returns a copy of the
string with leading and
trailing characters
removed
• If chars is omitted
or None, whitespace
characters are
removed
strip
8
© SkillBrew http://skillbrew.com
message = " enter the dragon "
message.lstrip()
Output:
'enter the dragon'
message = "--enter the dragon--"
message.lstrip('-')
Output:
'enter the dragon--'
lstrip(chars):
• returns a copy of the
string with leading
characters removed
• If chars is omitted
or None, whitespace
characters are
removed
lstrip
9
© SkillBrew http://skillbrew.com
message = " enter the dragon "
message.rstrip()
Output:
' enter the dragon'
message = "--enter the dragon--"
message.rstrip('-')
Output:
'--enter the dragon'
rstrip(chars):
• returns a copy of the
string with trailing
characters removed
• If chars is omitted
or None, whitespace
characters are
removed
rstrip
10
11
SPLITTING AND JOINING
STRINGS
© SkillBrew http://skillbrew.com
Splitting
12
message = "enter the dragon"
print message.split()
Output:
['enter', 'the', 'dragon']
split()returns a list of words of the string
© SkillBrew http://skillbrew.com
Splitting (2)
13
message = "enter-the-dragon"
print message.split('-')
Output:
['enter', 'the', 'dragon']
split(delimiter)
delimiter: character or characters which we want to
use to split the string, by default it will be space
© SkillBrew http://skillbrew.com
Joining
14
seq_list = ['enter', 'the', 'dragon']
print ''.join(seq_list)
Output:
enter the dragon
str.join()
returns a string in which the string elements of sequence have
been joined by str separator
© SkillBrew http://skillbrew.com
Joining (2)
15
seq_tuple = ('enter','the','dragon')
print '-'.join(seq_tuple)
Output:
enter-the-dragon
© SkillBrew http://skillbrew.com
splitting/joining example
16
Lets say you have a link
'foo.com/forum?filter=comments&num=20'
1. You have to separate out the querystring from
url
2. You have to then separate out key-value pairs in
querystring
3. Now update the filter to views and num to 15
and form the url again
© SkillBrew http://skillbrew.com
splitting/joining example (2)
17
>>> link = 'foo.com/forum?filter=comments&num=20'
>>> components = link.split('?')
>>> components
['foo.com/forum', 'filter=comments&num=20']
>>> url = components[0]
>>> qs = components[1]
>>> url
'foo.com/forum'
>>> qs
'filter=comments&num=20'
Step 1
Split the link using '?' as
delimiter to separate out
url and querystring
© SkillBrew http://skillbrew.com
splitting/joining example (3)
18
>>> qs
'filter=comments&num=20'
>>>params = qs.split('&')
>>> params
['filter=comments', 'num=20']
Step 2
Split the querystring
using '&' as delimiter
© SkillBrew http://skillbrew.com
splitting/joining example (4)
19
>>> params
['filter=comments', 'num=20']
>>> params[0] = 'filter=views'
>>> params[1] = 'num=15'
>>> params
['filter=views', 'num=15']
>>> qs = '&'.join(params)
>>> qs
'filter=views&num=15'
Step 3
Update the parameters and
join them using '&' as
delimiter to form
querystring
© SkillBrew http://skillbrew.com
splitting/joining example (5)
20
>>> url
'foo.com/forum'
>>> qs
'filter=views&num=15'
>>> '?'.join([url, qs])
'foo.com/forum?filter=views&num=15'
>>> link = '?'.join([url, qs])
>>> link
'foo.com/forum?filter=views&num=15'
Step 4
join url and querystring
using '?' as delimiter
Summary
 upper, lower, capitalize
 startswith and endswith
 strip, lstrip and rstrip
 splitting and joining strings
21
© SkillBrew http://skillbrew.com
Resources
 String methods python docs
http://docs.Python.org/2/library/stdtypes.html#string-
methods
22
23

More Related Content

PDF
Python strings
PPTX
Strings in Python
ODP
Python Modules
PPT
Python GUI Programming
PDF
Strings in python
PDF
Python basic
PPTX
Python OOPs
PDF
Variables & Data Types In Python | Edureka
Python strings
Strings in Python
Python Modules
Python GUI Programming
Strings in python
Python basic
Python OOPs
Variables & Data Types In Python | Edureka

What's hot (20)

PPT
programming with python ppt
PPTX
Python strings presentation
PPTX
Variables in python
PPTX
Python dictionary
PPTX
Chapter 05 classes and objects
PPTX
Java constructors
PPT
One Dimensional Array
PDF
Strings in java
PPT
Adv. python regular expression by Rj
PDF
Python Programming - XI. String Manipulation and Regular Expressions
PPTX
Python Functions
PDF
Python functions
PPTX
Python: Modules and Packages
PDF
Functions and modules in python
PPTX
Regular expressions in Python
PDF
Python : Regular expressions
PPTX
Class, object and inheritance in python
PDF
Methods in Java
programming with python ppt
Python strings presentation
Variables in python
Python dictionary
Chapter 05 classes and objects
Java constructors
One Dimensional Array
Strings in java
Adv. python regular expression by Rj
Python Programming - XI. String Manipulation and Regular Expressions
Python Functions
Python functions
Python: Modules and Packages
Functions and modules in python
Regular expressions in Python
Python : Regular expressions
Class, object and inheritance in python
Methods in Java
Ad

Viewers also liked (13)

PPTX
Python Programming Essentials - M1 - Course Introduction
PPTX
Python Programming Essentials - M9 - String Formatting
PPTX
Python Programming Essentials - M7 - Strings
PPTX
Python Programming Essentials - M4 - Editors and IDEs
PPTX
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
PPTX
Python Programming Essentials - M23 - datetime module
PPTX
Python Programming Essentials - M21 - Exception Handling
PPTX
Python Programming Essentials - M28 - Debugging with pdb
PPTX
Python Programming Essentials - M40 - Invoking External Programs
PPTX
Python Programming Essentials - M31 - PEP 8
PPTX
Python Programming Essentials - M44 - Overview of Web Development
PPTX
Python Programming Essentials - M20 - Classes and Objects
PDF
Why I Love Python V2
Python Programming Essentials - M1 - Course Introduction
Python Programming Essentials - M9 - String Formatting
Python Programming Essentials - M7 - Strings
Python Programming Essentials - M4 - Editors and IDEs
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M20 - Classes and Objects
Why I Love Python V2
Ad

Similar to Python Programming Essentials - M8 - String Methods (20)

PPTX
UNIT 4 python.pptx
PPTX
Python Programming-UNIT-II - Strings.pptx
PDF
python_strings.pdf
PDF
Strings3a4esrxdfgcbhjjjjjiiol;lkljiojoii
PDF
14-Strings-In-Python strings with oops .pdf
PPTX
stringggg.pptxtujd7ttttttttttttttttttttttttttttttttttt
PPT
Strings.ppt
PDF
python1uhaibueuhERADGAIUSAERUGHw9uSS.pdf
PPTX
STRINGS IN PYTHON
PDF
strings in python (presentation for DSA)
PPTX
Introduction To Programming with Python-3
PPTX
Python Strings.pptx
PPTX
Strings.pptxbfffffffffffffffffffffffffffffffffffffffffd
PPTX
PROGRAMMING FOR PROBLEM SOLVING FOR STRING CONCEPT
PPTX
Python Strings and its Featues Explained in Detail .pptx
PDF
Strings in Python
PDF
Python data handling
PPTX
Day5 String python language for btech.pptx
PPTX
string manipulation in python ppt for grade 11 cbse
UNIT 4 python.pptx
Python Programming-UNIT-II - Strings.pptx
python_strings.pdf
Strings3a4esrxdfgcbhjjjjjiiol;lkljiojoii
14-Strings-In-Python strings with oops .pdf
stringggg.pptxtujd7ttttttttttttttttttttttttttttttttttt
Strings.ppt
python1uhaibueuhERADGAIUSAERUGHw9uSS.pdf
STRINGS IN PYTHON
strings in python (presentation for DSA)
Introduction To Programming with Python-3
Python Strings.pptx
Strings.pptxbfffffffffffffffffffffffffffffffffffffffffd
PROGRAMMING FOR PROBLEM SOLVING FOR STRING CONCEPT
Python Strings and its Featues Explained in Detail .pptx
Strings in Python
Python data handling
Day5 String python language for btech.pptx
string manipulation in python ppt for grade 11 cbse

More from P3 InfoTech Solutions Pvt. Ltd. (20)

PPTX
Python Programming Essentials - M39 - Unit Testing
PPTX
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
PPTX
Python Programming Essentials - M35 - Iterators & Generators
PPTX
Python Programming Essentials - M34 - List Comprehensions
PPTX
Python Programming Essentials - M29 - Python Interpreter and Files
PPTX
Python Programming Essentials - M27 - Logging module
PPTX
Python Programming Essentials - M25 - os and sys modules
PPTX
Python Programming Essentials - M24 - math module
PPTX
Python Programming Essentials - M22 - File Operations
PPTX
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
PPTX
Python Programming Essentials - M18 - Modules and Packages
PPTX
Python Programming Essentials - M17 - Functions
PPTX
Python Programming Essentials - M16 - Control Flow Statements and Loops
PPTX
Python Programming Essentials - M15 - References
PPTX
Python Programming Essentials - M14 - Dictionaries
PPTX
Python Programming Essentials - M13 - Tuples
PPTX
Python Programming Essentials - M12 - Lists
PPTX
Python Programming Essentials - M11 - Comparison and Logical Operators
PPTX
Python Programming Essentials - M6 - Code Blocks and Indentation
PPTX
Python Programming Essentials - M5 - Variables
Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M24 - math module
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M15 - References
Python Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M13 - Tuples
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M5 - Variables

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Approach and Philosophy of On baking technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
A Presentation on Artificial Intelligence
PPTX
Big Data Technologies - Introduction.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
NewMind AI Monthly Chronicles - July 2025
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Approach and Philosophy of On baking technology
The AUB Centre for AI in Media Proposal.docx
Encapsulation_ Review paper, used for researhc scholars
Unlocking AI with Model Context Protocol (MCP)
Chapter 3 Spatial Domain Image Processing.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
A Presentation on Artificial Intelligence
Big Data Technologies - Introduction.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
NewMind AI Monthly Chronicles - July 2025

Python Programming Essentials - M8 - String Methods