SlideShare a Scribd company logo
Strings
 In programming terms, text is usually called string.
 A Python string is a sequence, which consists of zero or more characters.
 String is an immutable data structure, which means they cannot be changed.
 String is one of the most important data type in Python.
 String can be a single character or a combination of different characters.
 String literals in python are surrounded by either single quotation marks, or
double quotation marks.
'hello' is the same as "hello".
Examples
>>> a = "Hello, World!"
>>> print(a)
Hello, World!
Strings
 String is an immutable data structure, which means they cannot be
changed.
 Cannot edit the value of the variable although you can reassign it.
>>> name = “Ali Danish"
>>> name
Ali Danish
>>> name = “Jibran”
Jibran
Length and Concatenation of Strings
 To find the length of the string, len() function is used
 To join or concatenate strings, use the ‘+’ operator on strings
>>> name = "Ali Danish"
>>> name
'Ali Danish'
>>> len(name)
10
>>> name = "Ali Danish"
>>> prefix = "Mr. “
>>> prefix + name
'Mr. Ali Danish‘
Subscript Operator
 The subscript operator is defined as square brackets [].
 It is used to access the elements of string, list tuple, and so on.
 Syntax with index
<string>[index]
>>> name = "The Avengers"
>>> len(name)
12
>>> name[0]
'T'
>>> name[11]
's'
Slicing
Negative Indexing
>>> name = "The Avengers"
>>> name[-1]
's'
>>> name[-12]
'T'
>>> name[::-1]
'sregnevA ehT'
Slicing and Dicing
>>> name
'The Avengers'
>>> name[0:3]
'The‘
>>> name[:6]
'The Av'
>>> name[:]
'The Avengers'
>>> name[5:9]
'veng'
>>> name[::2]
'TeAegr'
String Case Methods
Method Expression Result
upper() name.upper() 'HELLO ALI’
lower() name.lower() 'hello ali'
capitalize() name.capitalize() ‘Hello ali'
swapcase() name.swapcase() 'hELLO aLI'
title name.title() 'Hello Ali'
name = “Hello Ali”
String Strip Methods
Method Expression Result
lstrip() name.rstrip() 'Dr Maqsood##'
rstrip() name.lstrip() '*Dr Maqsood'
strip() name.strip() 'Dr Maqsood'
name = “*Dr Maqsood##”
Split Method
Syntax: str.split(“delimiter”, num)
Split based on delimiter ‘-’
>>> str1 = "27-12-2016"
>>> str1.split("-“)
['27', '12’,’2016']
Split based on default space
>>> name = "Mohit raj"
>>> name.split()
['Mohit', 'raj']
Replace Method
syntax: str.replace(old, new max)
Relace returns copy of string with old characters are replaced with new ones
>>> str1 = "27-12-2016"
>>> str1 = "time is great and time is money"
>>> str1.replace("is","was")
'time was great and time was money‘
Join Method
syntax: str.join(seq)
seq contains sequence of separated strings here st1 acts as a separator.
>>> name = ["Mohit","raj"]
>>> " ".join(name)
'Mohit raj'
String Boolean Methods
endswith method
syntax: str.endswith(sub-string, begin,end]
>>> str1 = "Life should be great rather than long“
>>> str1.endswith("ng")
True
>>> str1.endswith("er")
False
>>> str1.endswith("er",0,27)
True
String Boolean Methods
startswith method
syntax: str.startswith(sub-string, begin,start]
>>> str1 = "Life should be great rather than long“
>>> str1.startswith("Li")
True
>>> str1.startswith("be", 11)
False
>>> str1.startswith("be", 12, 16)
True


More Boolean Methods
isalnum()
isdigit()
isspace()
islower()
isupper

More Related Content

PPTX
Chapter 11 Strings and methods [Autosaved].pptx
PPTX
Chapter 11 Strings.pptx
PPTX
Python Programming-UNIT-II - Strings.pptx
PPT
Introduction to Python Programming.ppt
PPTX
Python Workshop
PPT
PPTX
Detailed description of Strings in Python
PPTX
"Strings in Python - Presentation Slide"
Chapter 11 Strings and methods [Autosaved].pptx
Chapter 11 Strings.pptx
Python Programming-UNIT-II - Strings.pptx
Introduction to Python Programming.ppt
Python Workshop
Detailed description of Strings in Python
"Strings in Python - Presentation Slide"

Similar to Python Strings Basics with Common Methods.pptx (20)

PPT
Python programming tutorial for beginners
PPTX
array unit 1 and strings fffff .pptx
PDF
Python programming : Strings
PDF
Strings brief introduction in python.pdf
PPTX
Module-2_Strings concepts in c programming
PPT
Python tutorial
PPTX
Arrays in C++
PDF
Python revision tour II
PPTX
Chapter 14 strings
PDF
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf
PPTX
Python Strings.pptx
PDF
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
PPTX
Python Lecture 8
DOCX
Array assignment
PPTX
UNIT 4 python.pptx
PPTX
Array and string
PPT
Python tutorial
PPT
Python tutorial
PPT
Chapter 9 - Characters and Strings
PPTX
Arrays & Strings.pptx
Python programming tutorial for beginners
array unit 1 and strings fffff .pptx
Python programming : Strings
Strings brief introduction in python.pdf
Module-2_Strings concepts in c programming
Python tutorial
Arrays in C++
Python revision tour II
Chapter 14 strings
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf
Python Strings.pptx
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Python Lecture 8
Array assignment
UNIT 4 python.pptx
Array and string
Python tutorial
Python tutorial
Chapter 9 - Characters and Strings
Arrays & Strings.pptx
Ad

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Big Data Technologies - Introduction.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
Teaching material agriculture food technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Cloud computing and distributed systems.
PDF
Approach and Philosophy of On baking technology
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectral efficient network and resource selection model in 5G networks
Understanding_Digital_Forensics_Presentation.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25 Week I
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Big Data Technologies - Introduction.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Per capita expenditure prediction using model stacking based on satellite ima...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
cuic standard and advanced reporting.pdf
Programs and apps: productivity, graphics, security and other tools
Building Integrated photovoltaic BIPV_UPV.pdf
Teaching material agriculture food technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Review of recent advances in non-invasive hemoglobin estimation
Cloud computing and distributed systems.
Approach and Philosophy of On baking technology
Ad

Python Strings Basics with Common Methods.pptx

  • 1. Strings  In programming terms, text is usually called string.  A Python string is a sequence, which consists of zero or more characters.  String is an immutable data structure, which means they cannot be changed.  String is one of the most important data type in Python.  String can be a single character or a combination of different characters.  String literals in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello". Examples >>> a = "Hello, World!" >>> print(a) Hello, World!
  • 2. Strings  String is an immutable data structure, which means they cannot be changed.  Cannot edit the value of the variable although you can reassign it. >>> name = “Ali Danish" >>> name Ali Danish >>> name = “Jibran” Jibran
  • 3. Length and Concatenation of Strings  To find the length of the string, len() function is used  To join or concatenate strings, use the ‘+’ operator on strings >>> name = "Ali Danish" >>> name 'Ali Danish' >>> len(name) 10 >>> name = "Ali Danish" >>> prefix = "Mr. “ >>> prefix + name 'Mr. Ali Danish‘
  • 4. Subscript Operator  The subscript operator is defined as square brackets [].  It is used to access the elements of string, list tuple, and so on.  Syntax with index <string>[index] >>> name = "The Avengers" >>> len(name) 12 >>> name[0] 'T' >>> name[11] 's'
  • 5. Slicing Negative Indexing >>> name = "The Avengers" >>> name[-1] 's' >>> name[-12] 'T' >>> name[::-1] 'sregnevA ehT'
  • 6. Slicing and Dicing >>> name 'The Avengers' >>> name[0:3] 'The‘ >>> name[:6] 'The Av' >>> name[:] 'The Avengers' >>> name[5:9] 'veng' >>> name[::2] 'TeAegr'
  • 7. String Case Methods Method Expression Result upper() name.upper() 'HELLO ALI’ lower() name.lower() 'hello ali' capitalize() name.capitalize() ‘Hello ali' swapcase() name.swapcase() 'hELLO aLI' title name.title() 'Hello Ali' name = “Hello Ali”
  • 8. String Strip Methods Method Expression Result lstrip() name.rstrip() 'Dr Maqsood##' rstrip() name.lstrip() '*Dr Maqsood' strip() name.strip() 'Dr Maqsood' name = “*Dr Maqsood##”
  • 9. Split Method Syntax: str.split(“delimiter”, num) Split based on delimiter ‘-’ >>> str1 = "27-12-2016" >>> str1.split("-“) ['27', '12’,’2016'] Split based on default space >>> name = "Mohit raj" >>> name.split() ['Mohit', 'raj']
  • 10. Replace Method syntax: str.replace(old, new max) Relace returns copy of string with old characters are replaced with new ones >>> str1 = "27-12-2016" >>> str1 = "time is great and time is money" >>> str1.replace("is","was") 'time was great and time was money‘
  • 11. Join Method syntax: str.join(seq) seq contains sequence of separated strings here st1 acts as a separator. >>> name = ["Mohit","raj"] >>> " ".join(name) 'Mohit raj'
  • 12. String Boolean Methods endswith method syntax: str.endswith(sub-string, begin,end] >>> str1 = "Life should be great rather than long“ >>> str1.endswith("ng") True >>> str1.endswith("er") False >>> str1.endswith("er",0,27) True
  • 13. String Boolean Methods startswith method syntax: str.startswith(sub-string, begin,start] >>> str1 = "Life should be great rather than long“ >>> str1.startswith("Li") True >>> str1.startswith("be", 11) False >>> str1.startswith("be", 12, 16) True  