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

PPT
Schemaless Databases
PPTX
Rabin Karp ppt
PDF
Query trees
PDF
First order logic
PPT
Audio mining
PDF
The CAP Theorem
PPT
UML diagrams and symbols
PPTX
Gradient descent method
Schemaless Databases
Rabin Karp ppt
Query trees
First order logic
Audio mining
The CAP Theorem
UML diagrams and symbols
Gradient descent method

What's hot (20)

PDF
Inference in Bayesian Networks
PPTX
NP completeness
PPTX
AI Propositional logic
PPT
Uml class-diagram
PPT
Artificial Intelligence by B. Ravikumar
PDF
Feature selection
PPT
Learning sets of rules, Sequential Learning Algorithm,FOIL
PPTX
2.mathematics for machine learning
PPTX
Machine learning session4(linear regression)
PPT
Association rule mining
PDF
Recurrent Neural Networks
PDF
word2vec - From theory to practice
PDF
Complexity Explained: A brief intro to complex systems
PPTX
The Relational Model
PDF
Lecture 4 principles of parallel algorithm design updated
PDF
Word2Vec
PDF
Agile Methods - course notes
PPTX
Word embedding
PPTX
R programming presentation
PPTX
Parts of speech tagger
Inference in Bayesian Networks
NP completeness
AI Propositional logic
Uml class-diagram
Artificial Intelligence by B. Ravikumar
Feature selection
Learning sets of rules, Sequential Learning Algorithm,FOIL
2.mathematics for machine learning
Machine learning session4(linear regression)
Association rule mining
Recurrent Neural Networks
word2vec - From theory to practice
Complexity Explained: A brief intro to complex systems
The Relational Model
Lecture 4 principles of parallel algorithm design updated
Word2Vec
Agile Methods - course notes
Word embedding
R programming presentation
Parts of speech tagger
Ad

Viewers also liked (15)

PPTX
Python Programming Essentials - M9 - String Formatting
PPTX
Python Programming Essentials - M8 - String Methods
PDF
Debugging of (C)Python applications
PPTX
The scarlet letter
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 - M1 - Course Introduction
PDF
Web front end development introduction to html css and javascript
PPTX
Python Programming Essentials - M40 - Invoking External Programs
PPTX
Python Programming Essentials - M28 - Debugging with pdb
PPTX
Python Programming Essentials - M31 - PEP 8
PPTX
Python Programming Essentials - M44 - Overview of Web Development
PPTX
Using Quotes in Newswriting
Python Programming Essentials - M9 - String Formatting
Python Programming Essentials - M8 - String Methods
Debugging of (C)Python applications
The scarlet letter
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 - M1 - Course Introduction
Web front end development introduction to html css and javascript
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M44 - Overview of Web Development
Using Quotes in Newswriting
Ad

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

PPTX
Python Programming Essentials - M6 - Code Blocks and Indentation
PDF
3-Python Python oho pytho hdiwefjhdsjhds
PPTX
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
PPTX
Python knowledge ,......................
PPTX
Chapter1 python introduction syntax general
PDF
Introduction to python3.pdf
PPTX
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
PDF
lab-assgn-practical-file-xii-cs.pdf
PPTX
Python fundamentals
PPTX
Introduction to python.pptx
PDF
Basic Concepts in Python
PDF
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
PPTX
Introduction on basic python and it's application
PDF
Python Tutorial
PDF
Python tutorial
PPTX
Python component in mule
PPTX
unit (1)INTRODUCTION TO PYTHON course.pptx
PDF
basics of python programming5.pdf
PPT
intro to programming languge c++ for computer department
Python Programming Essentials - M6 - Code Blocks and Indentation
3-Python Python oho pytho hdiwefjhdsjhds
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
Python knowledge ,......................
Chapter1 python introduction syntax general
Introduction to python3.pdf
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
lab-assgn-practical-file-xii-cs.pdf
Python fundamentals
Introduction to python.pptx
Basic Concepts in Python
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
Introduction on basic python and it's application
Python Tutorial
Python tutorial
Python component in mule
unit (1)INTRODUCTION TO PYTHON course.pptx
basics of python programming5.pdf
intro to programming languge c++ for computer department

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 - M20 - Classes and Objects
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 - 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 - M20 - Classes and Objects
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 - M5 - Variables

Recently uploaded (20)

PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
Architecture types and enterprise applications.pdf
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
DOCX
search engine optimization ppt fir known well about this
PDF
Five Habits of High-Impact Board Members
PPTX
Build Your First AI Agent with UiPath.pptx
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PPTX
Benefits of Physical activity for teenagers.pptx
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
Statistics on Ai - sourced from AIPRM.pdf
PPT
What is a Computer? Input Devices /output devices
PDF
STKI Israel Market Study 2025 version august
PDF
A review of recent deep learning applications in wood surface defect identifi...
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PDF
Flame analysis and combustion estimation using large language and vision assi...
Taming the Chaos: How to Turn Unstructured Data into Decisions
Getting started with AI Agents and Multi-Agent Systems
A contest of sentiment analysis: k-nearest neighbor versus neural network
Developing a website for English-speaking practice to English as a foreign la...
Architecture types and enterprise applications.pdf
sbt 2.0: go big (Scala Days 2025 edition)
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
search engine optimization ppt fir known well about this
Five Habits of High-Impact Board Members
Build Your First AI Agent with UiPath.pptx
Enhancing plagiarism detection using data pre-processing and machine learning...
Benefits of Physical activity for teenagers.pptx
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
Statistics on Ai - sourced from AIPRM.pdf
What is a Computer? Input Devices /output devices
STKI Israel Market Study 2025 version august
A review of recent deep learning applications in wood surface defect identifi...
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
Flame analysis and combustion estimation using large language and vision assi...

Python Programming Essentials - M7 - Strings

Editor's Notes

  • #17: Since Strings are immutable therefore operations like updating and deleting a string do not work