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

PDF
Python 3.x quick syntax guide
Universiti Technologi Malaysia (UTM)
 
PPTX
Python Programming Essentials - M6 - Code Blocks and Indentation
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Interfaces and abstract classes
AKANSH SINGHAL
 
PPTX
Typedef
vaseemkhn
 
PPTX
Interface java
atiafyrose
 
PPTX
Class template
Kousalya M
 
PDF
Python quick guide1
Kanchilug
 
PPTX
Type conversion, precedence, associativity in c programming
Dhrumil Panchal
 
Python 3.x quick syntax guide
Universiti Technologi Malaysia (UTM)
 
Python Programming Essentials - M6 - Code Blocks and Indentation
P3 InfoTech Solutions Pvt. Ltd.
 
Interfaces and abstract classes
AKANSH SINGHAL
 
Typedef
vaseemkhn
 
Interface java
atiafyrose
 
Class template
Kousalya M
 
Python quick guide1
Kanchilug
 
Type conversion, precedence, associativity in c programming
Dhrumil Panchal
 

What's hot (20)

PPTX
Functions in c
sunila tharagaturi
 
DOCX
Functions oracle (pl/sql)
harman kaur
 
PDF
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
sudhakargeruganti
 
PPTX
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
PPSX
5bit field
Frijo Francis
 
PDF
Variables & Data Types In Python | Edureka
Edureka!
 
PPTX
Langchain Explained | What is Langchain? | Langchain Tutorial For Beginners |...
Simplilearn
 
PPTX
Abstract class in c++
Sujan Mia
 
PPTX
Abstraction in java
sawarkar17
 
PPTX
04. Console Input Output
Intro C# Book
 
PPTX
Java method
sunilchute1
 
ODP
OOP java
xball977
 
DOCX
Core programming in c
Rahul Pandit
 
PPTX
Data base connectivity and flex grid in vb
Amandeep Kaur
 
PPT
Inheritance : Extending Classes
Nilesh Dalvi
 
PPT
ORACLE 9i
suniljoshi151
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
C# in depth
Arnon Axelrod
 
PPTX
Constructors and destructors
Vineeta Garg
 
PDF
Generic Programming
Muhammad Alhalaby
 
Functions in c
sunila tharagaturi
 
Functions oracle (pl/sql)
harman kaur
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
sudhakargeruganti
 
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
5bit field
Frijo Francis
 
Variables & Data Types In Python | Edureka
Edureka!
 
Langchain Explained | What is Langchain? | Langchain Tutorial For Beginners |...
Simplilearn
 
Abstract class in c++
Sujan Mia
 
Abstraction in java
sawarkar17
 
04. Console Input Output
Intro C# Book
 
Java method
sunilchute1
 
OOP java
xball977
 
Core programming in c
Rahul Pandit
 
Data base connectivity and flex grid in vb
Amandeep Kaur
 
Inheritance : Extending Classes
Nilesh Dalvi
 
ORACLE 9i
suniljoshi151
 
Inheritance in java
Tech_MX
 
C# in depth
Arnon Axelrod
 
Constructors and destructors
Vineeta Garg
 
Generic Programming
Muhammad Alhalaby
 
Ad

Viewers also liked (15)

PPTX
Python Programming Essentials - M9 - String Formatting
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M8 - String Methods
P3 InfoTech Solutions Pvt. Ltd.
 
PDF
Debugging of (C)Python applications
Roman Podoliaka
 
PPTX
The scarlet letter
Jayshree Kunchala
 
PPTX
Python Programming Essentials - M4 - Editors and IDEs
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M23 - datetime module
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M21 - Exception Handling
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M1 - Course Introduction
P3 InfoTech Solutions Pvt. Ltd.
 
PDF
Web front end development introduction to html css and javascript
Marc Huang
 
PPTX
Python Programming Essentials - M40 - Invoking External Programs
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M28 - Debugging with pdb
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M31 - PEP 8
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python Programming Essentials - M44 - Overview of Web Development
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Using Quotes in Newswriting
CubReporters.org
 
Python Programming Essentials - M9 - String Formatting
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M8 - String Methods
P3 InfoTech Solutions Pvt. Ltd.
 
Debugging of (C)Python applications
Roman Podoliaka
 
The scarlet letter
Jayshree Kunchala
 
Python Programming Essentials - M4 - Editors and IDEs
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M23 - datetime module
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M21 - Exception Handling
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M1 - Course Introduction
P3 InfoTech Solutions Pvt. Ltd.
 
Web front end development introduction to html css and javascript
Marc Huang
 
Python Programming Essentials - M40 - Invoking External Programs
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M28 - Debugging with pdb
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M31 - PEP 8
P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M44 - Overview of Web Development
P3 InfoTech Solutions Pvt. Ltd.
 
Using Quotes in Newswriting
CubReporters.org
 
Ad

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

PDF
3-Python Python oho pytho hdiwefjhdsjhds
hardikbhagwaria83
 
PPTX
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
JessylyneSophia
 
PPTX
Python knowledge ,......................
sabith777a
 
PPTX
Chapter1 python introduction syntax general
ssuser77162c
 
PDF
Introduction to python3.pdf
Mohammed Aman Nawaz
 
PPTX
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
ANUSUYA S
 
PDF
lab-assgn-practical-file-xii-cs.pdf
JeevithaG22
 
PPTX
Python fundamentals
natnaelmamuye
 
PPTX
Introduction to python.pptx
pcjoshi02
 
PDF
Basic Concepts in Python
Sumit Satam
 
PDF
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
KosmikTech1
 
PPTX
Introduction on basic python and it's application
sriram2110
 
PDF
Python Tutorial
AkramWaseem
 
PDF
Python tutorial
Dominik KAszubowski
 
PPTX
Python component in mule
Ramakrishna kapa
 
PPTX
unit (1)INTRODUCTION TO PYTHON course.pptx
usvirat1805
 
PDF
basics of python programming5.pdf
Pushkaran3
 
PPT
intro to programming languge c++ for computer department
MemMem25
 
PDF
علم البيانات - Data Sience
App Ttrainers .com
 
3-Python Python oho pytho hdiwefjhdsjhds
hardikbhagwaria83
 
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
JessylyneSophia
 
Python knowledge ,......................
sabith777a
 
Chapter1 python introduction syntax general
ssuser77162c
 
Introduction to python3.pdf
Mohammed Aman Nawaz
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
ANUSUYA S
 
lab-assgn-practical-file-xii-cs.pdf
JeevithaG22
 
Python fundamentals
natnaelmamuye
 
Introduction to python.pptx
pcjoshi02
 
Basic Concepts in Python
Sumit Satam
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
KosmikTech1
 
Introduction on basic python and it's application
sriram2110
 
Python Tutorial
AkramWaseem
 
Python tutorial
Dominik KAszubowski
 
Python component in mule
Ramakrishna kapa
 
unit (1)INTRODUCTION TO PYTHON course.pptx
usvirat1805
 
basics of python programming5.pdf
Pushkaran3
 
intro to programming languge c++ for computer department
MemMem25
 
علم البيانات - Data Sience
App Ttrainers .com
 

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

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

Recently uploaded (20)

PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Architecture of the Future (09152021)
EdwardMeyman
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
Software Development Company | KodekX
KodekX
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Architecture of the Future (09152021)
EdwardMeyman
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 

Python Programming Essentials - M7 - Strings

Editor's Notes

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