SlideShare a Scribd company logo
1 of 23
http://www.skillbrew.com
/SkillbrewTalent brewed by the
industry itself
Strings
Pavan Verma
Python Programming Essentials
@YinYangPavan
© SkillBrew http://skillbrew.com
What is a String
 A string is a sequence of characters
message = "Welcome to Python"
2
© SkillBrew http://skillbrew.com
Access a character in string
3
message = 'Welcome to Python'
print message[0]
print message[1]
print message[2]
print message[3]
print message[4]
Output:
W
e
l
c
o
str[index]
© SkillBrew http://skillbrew.com
Negative Indexes
4
message = 'Welcome to Python'
print message[-1]
print message[-2]
print message[-3]
print message[-4]
Output:
n
o
h
t
str[-index]
© SkillBrew http://skillbrew.com
Length of a string
5
message = 'Welcome to Python'
print len(message)
Output:
17
len(string)
© SkillBrew http://skillbrew.com
Single quotes Vs Double quotes
 You can use either single quotes or double
quotes for string literals – they are the same
6
>>> text = 'some text'
>>> text = "some text"
© SkillBrew http://skillbrew.com
Single quotes Vs Double quotes (2)
 Need to escape double quotes in double quoted
strings
• Use single quotes for strings that contain double quotes
>>> s = "He said "Hello""
>>> s
'He said "Hello"'
 Need to escape single quotes in single quoted strings
• Use double quotes for strings that contain single quotes
>>> s = 'You've got an error!'
>>> s
"You've got an error!"
7
© SkillBrew http://skillbrew.com
Triple quoted strings
 Python also has triple quoted strings available
 In some cases, when you need to include really long
string using triple quoted strings is useful
>>> message = """
This is a multi line message
use triple quotes if the text is too long
"""
8
© SkillBrew http://skillbrew.com
Triple quoted strings (2)
 You can also uses triple single quotes, there is no
difference between single triple quoted strings and
double triple quoted strings
>>> message = '''
This is a multi line message
use triple quotes if the text is too long
'''
9
Triple quoted strings are also used as Docstrings which will be
covered in functions
© SkillBrew http://skillbrew.com
Note for C/C++ Programmers
 There is no separate char data type in Python
 In Python, a character is just a string of length 1
eg: text ='f'
10
© SkillBrew http://skillbrew.com
Note for Perl/PHP Programmers
 Remember that single-quoted strings and double-
quoted strings are the same – they do not differ in
any significant way
11
© SkillBrew http://skillbrew.com
String Concatenation
12
>>> 'foo' + 'bar'
'foobar'
>>> 'foo' + 'bar' + '123'
'foobar123'
>>> name = 'Monty'
>>> last_name = 'Python'
>>> name + last_name
'MontyPython'
+ operator is used to
concatenate strings
© SkillBrew http://skillbrew.com
String Concatenation (2)
13
>>> 'foo' + 'bar' + 123
TypeError: cannot concatenate 'str' and
'int' objects
string concatenation does not works with other types
© SkillBrew http://skillbrew.com
String Concatenation (2)
14
>>> 'foo' + 'bar' + str(123)
'foobar123'
Use built in str() function to convert to a string
© SkillBrew http://skillbrew.com
Strings are immutable
15
>>> message = 'Python is awesome'
>>> message[0] = 'j'
TypeError: 'str' object does not support
item assignment
>>> message = 'Python is awesome'
>>> del message[0]
TypeError: 'str' object does not support
item deletion.
Python strings
cannot be changed
© SkillBrew http://skillbrew.com
Strings are immutable (2)
16
Strings are immutable but that does not mean the variable
cannot change, variable can point to anything
>>> message = 'Python is awesome'
>>> message
'Python is awesome'
>>> message = 'Python is dynamicaly
typed'
>>> message
'Python is dynamicaly typed'
© SkillBrew http://skillbrew.com
What is Slicing
17
slicing in Python is powerful way of extracting sub-parts of
a string, lists, tuples
Use Case:
You can use slicing to extract sub-string out of a
string
© SkillBrew http://skillbrew.com
Slicing
18
message = 'Python is awesome'
print message[0:5]
print message[7:10]
print message[10:17]
print message[:]
print message[5:]
print message[:6]
Outputs:
Pytho
is
awesome
Python is awesome
n is awesome
Python
str[start:end]
start: substring starts from this element
end: end of substring excluding the element at this index
© SkillBrew http://skillbrew.com
Slicing (2)
19
str[start:end]
1. Slicing always returns a new string. Remember strings are
immutable
2. If you don’t provide start the substring starts from the beginning
of the string. eg: message[:5]
3. If end is not provided the substring runs till the end of the
string
4. If both start and end are missing the entire string is returned
© SkillBrew http://skillbrew.com
in operator
in is a Boolean operator which takes two strings
and returns True if the first string is a sub-string
of the second string, False otherwise
't' in 'Welcome to Python'
True
'Python' in 'Welcome to Python'
True
'Python' in 'Welcome to Python'
True
20
Summary
 What is a string
 Access characters in a string
 Negative indexes
 Length of string
 Single quotes Vs Double quotes
 Triple quoted strings
 String concatenation
 Strings are Immutable
 in operator
21
© SkillBrew http://skillbrew.com
Resources
 Tutorial on python strings
http://www.tutorialspoint.com/Python/Python_strings.htm
 Single vs double strings
http://docs.ckan.org/en/latest/Python-coding-standards.html
22
23

More Related Content

What's hot (20)

Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Program control statements in c#
Program control statements in c#Program control statements in c#
Program control statements in c#
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Java interface
Java interfaceJava interface
Java interface
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Java IO
Java IOJava IO
Java IO
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Java Lambda Expressions.pptx
Java Lambda Expressions.pptxJava Lambda Expressions.pptx
Java Lambda Expressions.pptx
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 

Viewers also liked

Debugging of (C)Python applications
Debugging of (C)Python applicationsDebugging of (C)Python applications
Debugging of (C)Python applicationsRoman Podoliaka
 
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 OperatorsP3 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 HandlingP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M1 - Course Introduction
Python Programming Essentials - M1 - Course IntroductionPython Programming Essentials - M1 - Course Introduction
Python Programming Essentials - M1 - Course IntroductionP3 InfoTech Solutions Pvt. Ltd.
 
Web front end development introduction to html css and javascript
Web front end development introduction to html css and javascriptWeb front end development introduction to html css and javascript
Web front end development introduction to html css and javascriptMarc Huang
 
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 ProgramsP3 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 pdbP3 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 DevelopmentP3 InfoTech Solutions Pvt. Ltd.
 
Using Quotes in Newswriting
Using Quotes in NewswritingUsing Quotes in Newswriting
Using Quotes in NewswritingCubReporters.org
 

Viewers also liked (15)

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 - M8 - String Methods
Python Programming Essentials - M8 - String MethodsPython Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String Methods
 
Debugging of (C)Python applications
Debugging of (C)Python applicationsDebugging of (C)Python applications
Debugging of (C)Python applications
 
The scarlet letter
The scarlet letterThe scarlet letter
The scarlet letter
 
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 - M23 - datetime module
Python Programming Essentials - M23 - datetime modulePython Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime module
 
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 - M1 - Course Introduction
Python Programming Essentials - M1 - Course IntroductionPython Programming Essentials - M1 - Course Introduction
Python Programming Essentials - M1 - Course Introduction
 
Web front end development introduction to html css and javascript
Web front end development introduction to html css and javascriptWeb front end development introduction to html css and javascript
Web front end development introduction to html css and javascript
 
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 - 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 - 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
 
Using Quotes in Newswriting
Using Quotes in NewswritingUsing Quotes in Newswriting
Using Quotes in Newswriting
 

Similar to Python Programming Essentials - M7 - Strings

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 IndentationP3 InfoTech Solutions Pvt. Ltd.
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...ANUSUYA S
 
lab-assgn-practical-file-xii-cs.pdf
lab-assgn-practical-file-xii-cs.pdflab-assgn-practical-file-xii-cs.pdf
lab-assgn-practical-file-xii-cs.pdfJeevithaG22
 
Introduction to python.pptx
Introduction to python.pptxIntroduction to python.pptx
Introduction to python.pptxpcjoshi02
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in PythonSumit Satam
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfKosmikTech1
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++cpjcollege
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxusvirat1805
 
basics of python programming5.pdf
basics of python programming5.pdfbasics of python programming5.pdf
basics of python programming5.pdfPushkaran3
 
علم البيانات - Data Sience
علم البيانات - Data Sience علم البيانات - Data Sience
علم البيانات - Data Sience App Ttrainers .com
 
python presentation
python presentationpython presentation
python presentationVaibhavMawal
 
Python Programming by Dr B P Sharma for Everyone
Python Programming by Dr B P Sharma for  EveryonePython Programming by Dr B P Sharma for  Everyone
Python Programming by Dr B P Sharma for Everyoneinfo560863
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projectsPVS-Studio
 

Similar to Python Programming Essentials - M7 - Strings (20)

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
 
Introduction to python3.pdf
Introduction to python3.pdfIntroduction to python3.pdf
Introduction to python3.pdf
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
 
lab-assgn-practical-file-xii-cs.pdf
lab-assgn-practical-file-xii-cs.pdflab-assgn-practical-file-xii-cs.pdf
lab-assgn-practical-file-xii-cs.pdf
 
Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
 
Introduction to python.pptx
Introduction to python.pptxIntroduction to python.pptx
Introduction to python.pptx
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Python component in mule
Python component in mulePython component in mule
Python component in mule
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
 
basics of python programming5.pdf
basics of python programming5.pdfbasics of python programming5.pdf
basics of python programming5.pdf
 
علم البيانات - Data Sience
علم البيانات - Data Sience علم البيانات - Data Sience
علم البيانات - Data Sience
 
python presentation
python presentationpython presentation
python presentation
 
Python Programming by Dr B P Sharma for Everyone
Python Programming by Dr B P Sharma for  EveryonePython Programming by Dr B P Sharma for  Everyone
Python Programming by Dr B P Sharma for Everyone
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 

More from 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 ConceptsP3 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 & GeneratorsP3 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 ComprehensionsP3 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 FilesP3 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 modulesP3 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 ObjectsP3 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 PackagesP3 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 LoopsP3 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 OperatorsP3 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 - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsPython Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and Objects
 
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 - M5 - Variables
Python Programming Essentials - M5 - VariablesPython Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - Variables
 

Recently uploaded

Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 

Recently uploaded (20)

Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 

Python Programming Essentials - M7 - Strings

  • 1. http://www.skillbrew.com /SkillbrewTalent brewed by the industry itself Strings Pavan Verma Python Programming Essentials @YinYangPavan
  • 2. © SkillBrew http://skillbrew.com What is a String  A string is a sequence of characters message = "Welcome to Python" 2
  • 3. © SkillBrew http://skillbrew.com Access a character in string 3 message = 'Welcome to Python' print message[0] print message[1] print message[2] print message[3] print message[4] Output: W e l c o str[index]
  • 4. © SkillBrew http://skillbrew.com Negative Indexes 4 message = 'Welcome to Python' print message[-1] print message[-2] print message[-3] print message[-4] Output: n o h t str[-index]
  • 5. © SkillBrew http://skillbrew.com Length of a string 5 message = 'Welcome to Python' print len(message) Output: 17 len(string)
  • 6. © SkillBrew http://skillbrew.com Single quotes Vs Double quotes  You can use either single quotes or double quotes for string literals – they are the same 6 >>> text = 'some text' >>> text = "some text"
  • 7. © SkillBrew http://skillbrew.com Single quotes Vs Double quotes (2)  Need to escape double quotes in double quoted strings • Use single quotes for strings that contain double quotes >>> s = "He said "Hello"" >>> s 'He said "Hello"'  Need to escape single quotes in single quoted strings • Use double quotes for strings that contain single quotes >>> s = 'You've got an error!' >>> s "You've got an error!" 7
  • 8. © SkillBrew http://skillbrew.com Triple quoted strings  Python also has triple quoted strings available  In some cases, when you need to include really long string using triple quoted strings is useful >>> message = """ This is a multi line message use triple quotes if the text is too long """ 8
  • 9. © SkillBrew http://skillbrew.com Triple quoted strings (2)  You can also uses triple single quotes, there is no difference between single triple quoted strings and double triple quoted strings >>> message = ''' This is a multi line message use triple quotes if the text is too long ''' 9 Triple quoted strings are also used as Docstrings which will be covered in functions
  • 10. © SkillBrew http://skillbrew.com Note for C/C++ Programmers  There is no separate char data type in Python  In Python, a character is just a string of length 1 eg: text ='f' 10
  • 11. © SkillBrew http://skillbrew.com Note for Perl/PHP Programmers  Remember that single-quoted strings and double- quoted strings are the same – they do not differ in any significant way 11
  • 12. © SkillBrew http://skillbrew.com String Concatenation 12 >>> 'foo' + 'bar' 'foobar' >>> 'foo' + 'bar' + '123' 'foobar123' >>> name = 'Monty' >>> last_name = 'Python' >>> name + last_name 'MontyPython' + operator is used to concatenate strings
  • 13. © SkillBrew http://skillbrew.com String Concatenation (2) 13 >>> 'foo' + 'bar' + 123 TypeError: cannot concatenate 'str' and 'int' objects string concatenation does not works with other types
  • 14. © SkillBrew http://skillbrew.com String Concatenation (2) 14 >>> 'foo' + 'bar' + str(123) 'foobar123' Use built in str() function to convert to a string
  • 15. © SkillBrew http://skillbrew.com Strings are immutable 15 >>> message = 'Python is awesome' >>> message[0] = 'j' TypeError: 'str' object does not support item assignment >>> message = 'Python is awesome' >>> del message[0] TypeError: 'str' object does not support item deletion. Python strings cannot be changed
  • 16. © SkillBrew http://skillbrew.com Strings are immutable (2) 16 Strings are immutable but that does not mean the variable cannot change, variable can point to anything >>> message = 'Python is awesome' >>> message 'Python is awesome' >>> message = 'Python is dynamicaly typed' >>> message 'Python is dynamicaly typed'
  • 17. © SkillBrew http://skillbrew.com What is Slicing 17 slicing in Python is powerful way of extracting sub-parts of a string, lists, tuples Use Case: You can use slicing to extract sub-string out of a string
  • 18. © SkillBrew http://skillbrew.com Slicing 18 message = 'Python is awesome' print message[0:5] print message[7:10] print message[10:17] print message[:] print message[5:] print message[:6] Outputs: Pytho is awesome Python is awesome n is awesome Python str[start:end] start: substring starts from this element end: end of substring excluding the element at this index
  • 19. © SkillBrew http://skillbrew.com Slicing (2) 19 str[start:end] 1. Slicing always returns a new string. Remember strings are immutable 2. If you don’t provide start the substring starts from the beginning of the string. eg: message[:5] 3. If end is not provided the substring runs till the end of the string 4. If both start and end are missing the entire string is returned
  • 20. © SkillBrew http://skillbrew.com in operator in is a Boolean operator which takes two strings and returns True if the first string is a sub-string of the second string, False otherwise 't' in 'Welcome to Python' True 'Python' in 'Welcome to Python' True 'Python' in 'Welcome to Python' True 20
  • 21. Summary  What is a string  Access characters in a string  Negative indexes  Length of string  Single quotes Vs Double quotes  Triple quoted strings  String concatenation  Strings are Immutable  in operator 21
  • 22. © SkillBrew http://skillbrew.com Resources  Tutorial on python strings http://www.tutorialspoint.com/Python/Python_strings.htm  Single vs double strings http://docs.ckan.org/en/latest/Python-coding-standards.html 22
  • 23. 23

Editor's Notes

  1. Since Strings are immutable therefore operations like updating and deleting a string do not work