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

What's hot

Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
nitamhaske
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
Jussi Pohjolainen
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Maliha Mehr
 
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime modulePython Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime module
P3 InfoTech Solutions Pvt. Ltd.
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI Programming
RTS Tech
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
Nilesh Dalvi
 
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Edureka!
 
Python-Tuples
Python-TuplesPython-Tuples
Python-Tuples
Krishna Nanda
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
Function C programming
Function C programmingFunction C programming
Function C programming
Appili Vamsi Krishna
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
VedaGayathri1
 
C++
C++C++
Strings IN C
Strings IN CStrings IN C
Strings IN C
yndaravind
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
Intro C# Book
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 
Generics in java
Generics in javaGenerics in java
Generics in java
suraj pandey
 
Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python
Jaganadh Gopinadhan
 
C++ oop
C++ oopC++ oop
C++ oop
Sunil OS
 

What's hot (20)

Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime modulePython Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime module
 
Python GUI Programming
Python GUI ProgrammingPython GUI Programming
Python GUI Programming
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
 
Python-Tuples
Python-TuplesPython-Tuples
Python-Tuples
 
Function in C
Function in CFunction in C
Function in C
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
C++
C++C++
C++
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python
 
C++ oop
C++ oopC++ oop
C++ oop
 

Viewers also liked

Python Programming Essentials - M1 - Course Introduction
Python Programming Essentials - M1 - Course IntroductionPython Programming Essentials - M1 - Course Introduction
Python Programming Essentials - M1 - Course Introduction
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String FormattingPython Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String Formatting
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M7 - Strings
Python Programming Essentials - M7 - StringsPython Programming Essentials - M7 - Strings
Python Programming Essentials - M7 - Strings
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M4 - Editors and IDEs
Python Programming Essentials - M4 - Editors and IDEsPython Programming Essentials - M4 - Editors and IDEs
Python Programming Essentials - M4 - Editors and IDEs
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic OperatorsPython Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception Handling
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdbPython Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdb
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External ProgramsPython Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External Programs
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M31 - PEP 8Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M31 - PEP 8
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web DevelopmentPython Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web Development
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsPython Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and Objects
P3 InfoTech Solutions Pvt. Ltd.
 
Why I Love Python V2
Why I Love Python V2Why I Love Python V2
Why I Love Python V2
gsroma
 

Viewers also liked (12)

Python Programming Essentials - M1 - Course Introduction
Python Programming Essentials - M1 - Course IntroductionPython Programming Essentials - M1 - Course Introduction
Python Programming Essentials - M1 - Course Introduction
 
Python Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String FormattingPython Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String Formatting
 
Python Programming Essentials - M7 - Strings
Python Programming Essentials - M7 - StringsPython Programming Essentials - M7 - Strings
Python Programming Essentials - M7 - Strings
 
Python Programming Essentials - M4 - Editors and IDEs
Python Programming Essentials - M4 - Editors and IDEsPython Programming Essentials - M4 - Editors and IDEs
Python Programming Essentials - M4 - Editors and IDEs
 
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic OperatorsPython Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception Handling
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdbPython Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdb
 
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External ProgramsPython Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External Programs
 
Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M31 - PEP 8Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M31 - PEP 8
 
Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web DevelopmentPython Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web Development
 
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsPython Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and Objects
 
Why I Love Python V2
Why I Love Python V2Why I Love Python V2
Why I Love Python V2
 

Similar to Python Programming Essentials - M8 - String Methods

9 character string & string library
9  character string & string library9  character string & string library
9 character string & string library
MomenMostafa
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
Intro C# Book
 
Computer Programming- Lecture 6
Computer Programming- Lecture 6Computer Programming- Lecture 6
Computer Programming- Lecture 6
Dr. Md. Shohel Sayeed
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
paijitk
 
String functions
String functionsString functions
String functions
ssuser93a21b
 
String in c programming
String in c programmingString in c programming
String in c programming
Devan Thakur
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
php string part 4
php string part 4php string part 4
php string part 4
monikadeshmane
 
Python strings
Python stringsPython strings
Python strings
Aswini Dharmaraj
 
Python
PythonPython
Python
Wei-Bo Chen
 
The bones of a nice Python script
The bones of a nice Python scriptThe bones of a nice Python script
The bones of a nice Python script
saniac
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
Rumman Ansari
 
String notes
String notesString notes
String notes
Prasadu Peddi
 
Different uses of String in Python.pptx
Different uses of  String in Python.pptxDifferent uses of  String in Python.pptx
Different uses of String in Python.pptx
AryadipDey
 
Strings and Characters
Strings and CharactersStrings and Characters
Strings and Characters
Andy Juan Sarango Veliz
 
Strings
StringsStrings
Strings
Dhiviya Rose
 
Strings in python
Strings in pythonStrings in python
Strings in python
Prabhakaran V M
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Sway Wang
 

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

9 character string & string library
9  character string & string library9  character string & string library
9 character string & string library
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
Computer Programming- Lecture 6
Computer Programming- Lecture 6Computer Programming- Lecture 6
Computer Programming- Lecture 6
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
String functions
String functionsString functions
String functions
 
String in c programming
String in c programmingString in c programming
String in c programming
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
php string part 4
php string part 4php string part 4
php string part 4
 
Python strings
Python stringsPython strings
Python strings
 
Python
PythonPython
Python
 
The bones of a nice Python script
The bones of a nice Python scriptThe bones of a nice Python script
The bones of a nice Python script
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
String notes
String notesString notes
String notes
 
Different uses of String in Python.pptx
Different uses of  String in Python.pptxDifferent uses of  String in Python.pptx
Different uses of String in Python.pptx
 
Strings and Characters
Strings and CharactersStrings and Characters
Strings and Characters
 
Strings
StringsStrings
Strings
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 

More from P3 InfoTech Solutions Pvt. Ltd.

Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit TestingPython Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit Testing
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsPython Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & Generators
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsPython Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List Comprehensions
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and FilesPython Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and Files
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging modulePython Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging module
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesPython Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modules
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math modulePython Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math module
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M22 - File OperationsPython Programming Essentials - M22 - File Operations
Python Programming Essentials - M22 - File Operations
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesPython Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and Packages
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - FunctionsPython Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - Functions
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M15 - References
Python Programming Essentials - M15 - ReferencesPython Programming Essentials - M15 - References
Python Programming Essentials - M15 - References
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M14 - DictionariesPython Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M14 - Dictionaries
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M13 - Tuples
Python Programming Essentials - M13 - TuplesPython Programming Essentials - M13 - Tuples
Python Programming Essentials - M13 - Tuples
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - ListsPython Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - Lists
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsPython Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical Operators
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationPython Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and Indentation
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - VariablesPython Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - Variables
P3 InfoTech Solutions Pvt. Ltd.
 

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

Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit TestingPython Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit Testing
 
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
 
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsPython Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & Generators
 
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsPython Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List Comprehensions
 
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and FilesPython Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and Files
 
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging modulePython Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging module
 
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesPython Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modules
 
Python Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math modulePython Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math module
 
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M22 - File OperationsPython Programming Essentials - M22 - File Operations
Python Programming Essentials - M22 - File Operations
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
 
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesPython Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and Packages
 
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - FunctionsPython Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - Functions
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
 
Python Programming Essentials - M15 - References
Python Programming Essentials - M15 - ReferencesPython Programming Essentials - M15 - References
Python Programming Essentials - M15 - References
 
Python Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M14 - DictionariesPython Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M14 - Dictionaries
 
Python Programming Essentials - M13 - Tuples
Python Programming Essentials - M13 - TuplesPython Programming Essentials - M13 - Tuples
Python Programming Essentials - M13 - Tuples
 
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - ListsPython Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - Lists
 
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical OperatorsPython Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M11 - Comparison and Logical Operators
 
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationPython Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and Indentation
 
Python Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - VariablesPython Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - Variables
 

Recently uploaded

Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 

Recently uploaded (20)

Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 

Python Programming Essentials - M8 - String Methods

  • 1. http://www.skillbrew.com /SkillbrewTalent brewed by the industry itself Common String Methods Pavan Verma Python Programming Essentials @YinYangPavan
  • 2. © SkillBrew http://skillbrew.com Common String Methods  upper()  lower()  capitalize()  startswith()  endswith()  strip()  find()  split()  join() 2
  • 3. © 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
  • 4. © 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 >>>
  • 5. © 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
  • 6. © 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
  • 7. © 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
  • 8. © 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
  • 9. © 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
  • 10. © 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
  • 12. © 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
  • 13. © 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
  • 14. © 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
  • 15. © SkillBrew http://skillbrew.com Joining (2) 15 seq_tuple = ('enter','the','dragon') print '-'.join(seq_tuple) Output: enter-the-dragon
  • 16. © 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
  • 17. © 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
  • 18. © 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
  • 19. © 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
  • 20. © 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
  • 21. Summary  upper, lower, capitalize  startswith and endswith  strip, lstrip and rstrip  splitting and joining strings 21
  • 22. © SkillBrew http://skillbrew.com Resources  String methods python docs http://docs.Python.org/2/library/stdtypes.html#string- methods 22
  • 23. 23