SlideShare a Scribd company logo
STRINGS
Like many other popular
programming languages, strings in
Python are arrays of bytes representing
Unicode characters. However, Python does
not have a character data type, a single
character is simply a string with a length of
1. Square brackets can be used to access
elements of the string.
STRINGS
How to change or delete a string?
Strings are immutable. This
means that elements of a string cannot be
changed once it has been assigned. We can
simply reassign different strings to the
same name.
>>> my_string = 'programiz'
>>> my_string[5] = 'a'
TypeError: 'str' object does not support
item assignment.
How to change or delete a string?
How to create a string in Python?
How to create a string in Python?
Strings can be created by enclosing
characters inside a single quote or double
quotes. Even triple quotes can be used in
Python but generally used to represent
multiline strings and docstrings.
STRINGS
REPRESENTATION OF STRING
REPRESENTATION OF STRING
>>> s = “Hello Python”
This is how Python would index the string:
Forward Indexing
Backward Indexing
STRINGS – Programming Example
STRINGS - Example
Output Next Slide...
STRINGS - Example
How to access characters in a string?
How to access characters in a string?
We can access individual characters
using indexing and a range of characters
using slicing. Index starts from 0. Trying to
access a character out of index range will
raise an IndexError. The index must be an
integer. We can't use float or other types,
this will result into TypeError.
STRINGS
How to access characters in a string?
Python allows negative indexing for
its sequences.
The index of -1 refers to the last item,
-2 to the second last item and so on. We
can access a range of items in a string by
using the slicing operator (colon).
STRINGS
STRINGS – Programming Example
STRINGS
STRINGS
SLICING STRINGS EXAMPLES
For example:
>>>“Program”[3:5]
will result in:
‘gr ’
>>>“Program”[3:6]
will yield:
‘gra’
>>>p = “Program”
>>>p [:4]
‘Prog’
>>>p = “Program”
>>>p [4:]
‘ram’
>>>p = “Program”
>>>p [3:6]
‘gra’
Index Error!
STRINGS –Index error
More Functionality of String
More Functionality of String
>>> len(“Sainik School”)
13
>>>print(“Sainik” + “School”)
Sainik School
>>>print(“A” * 4 )
AAAA
>>>”sa” in “sainik”
True
>>>”pr” in “computer”
False
Finding Length of string
String Concatenation
String Repeat
Substring Tests
More Functionality of String
>>> name1="computer"
>>> name2=name1[3:5]
>>>name2
pu String Slicing
String Methods
String Methods
In Python, a method is a function that is
defined with respect to a particular object.
Syntax:
object.method(arguments)
For Example:
>>>name=“Classic”
>>>name.find(“s”)
3
the first position
where “s” appears
String object String Method Method Argument
Built in Methods
1. capitalize() Method
Capitalizes first letter of string
First letter capitalization
2. lstrip() & 3. rstrip() Methods
lstrip() method is used to remove left
padded spaces in a given string
>>>name1=“ a “
>>>name1.lstrip()
‘a ‘
rstrip() method is used to remove right
padded spaces in a given string
>>>name1.rstrip()
‘ a’
Removing left spaces
Removing right spaces
4. strip() Method
strip() method is used to remove left
and right padded spaces in a given string
>>>name1=“ a “
>>>name1.strip()
‘a‘
Removing left and right spaces for a given string
5. lower() Method
lower() method is used to convert
given string in to lower case.
>>>name1=“ SAINIK “
>>>name1.lower()
sainik
Converting in to lower case
6. upper() Method
upper() method is used to convert
given string in to upper case.
>>>name1=“ sainik “
>>>name1.upper()
SAINIK
Converting in to upper case
7. title() Method
title() method is used to convert given
string in to title case. Every first chatacter of
word of a given string is converted to title
case.
>>>name1=“ praveen murigeppa jigajinni“
>>>name1.title()
Praveen Murigeppa Jigajinni
In every word first letter is capitalized
8. swapcase() Method
swapcase() method is toggle the case.
Meaining upper to lower and lower to upper
case.
>>>name1=“ PrAvEeN“
>>>name1.swapcase()
pRaVeEn
Every character case is changed
9. ljust() Method
ljust() method is used to add spaces to
the left side of the given string
>>>name1=“ Jigajinni“
>>>name1.ljust(15)
‘Jigajinni ’
Left side padded with spaces
Note: string length is 9 and 6 spaces
added to the left side of string
10. rjust() Method
rjust() method is used to add spaces to
the right side of the given string
>>>name1=“ Jigajinni“
>>>name1.rjust(15)
‘ Jigajinni’
Right side padded with spaces
Note: string length is 9 and 6 spaces
added to the right side of string
11. center(width, fillchar) Method
The method center() returns centered
in a string of length width. Padding is done
using the specified fillchar. Default filler is a
space.
Centered string
12. zfill() Method
zfill() method is used to fill the zero to
a given string
>>>name1=“ 123“
>>>name1.zfill(15)
‘00123 ’
Filling Zeros
13. find() Method
find() method is used to find a
perticular character or string in a given
string.
>>> name1="praveen"
>>> name1.find("e")
4
e is present at 4th location (first appearance)
in a given string
14. count() Method
count() method is used to the number
of times character or string appears in a
given string.
>>>name1=“praveen“
>>>name1.count(“e”)
2
2 times e appears in a given string
15. stratswith() Method
startswith() method is used check
string start with particular string or not
>>>name1=“praveen“
>>>name1.startswith(“a”)
False
Given string not starting with “a”
16. endswith() Method
endswith() method is used check string
ends with particular string or not
>>>name1=“praveen“
>>>name1.endswith(“en”)
True
Given string ends with “en”
17. isdigit() Method
isdigit() method is used check string is
digit (number) or not and returns Boolean
value true or false.
>>>name2=“123”
>>>name2.isdigit()
True
>>name1=“123keyboard“
>>name1.isdigit()
False
Given string not number so false
Given string is
number
18. isnumeric() Method
isnumeric() is similar to isdigit()
method and is used check string is digit
(number) or not and returns Boolean value
true or false.
>>>name2=“123”
>>>name2.isnumeric()
True
>>name1=“123keyboard“
>>name1.isnumeric()
False
Given string not number so false
Given string is
number
19. isdecimal() Method
isnumeric(),isdigit() and isdecimal()
methods are used to check string is digit
(number) or not and returns Boolean value
true or false.
>>>name2=“123”
>>>name2.decimal()
True
>>name1=“123keyboard“
>>name1.isnumeric()
False
Given string not number so false
Given string is
number
20. isalpha() Method
isalpha() method is used check string is
digit or not and returns Boolean value true or
false.
>>>name2=“123”
>>>name2.isalpha()
False
>>name1=“123computer“
>>name1.isalpha()
False
>>>name3=“Keynoard”
>>>Name3.isaplpha()
True
Given string not
string it contains
digits
Given string does
not contain string
It’s a string
21. isalnum() Method
isalnum() method is used check string is
alpha numeric string or not.
>>>name2=“123”
>>>name2.isalnum()
True
>>>name1=“123computer“
>>>name1.isalnum()
True
>>>name3=“Praveen”
>>>name3.isalnum()
True
Given string is
alpha numeric
22. islower() Method
islower() method is used check string
contains all lower case letters or not, it returns
true or false result.
>>>name2=“Praveen”
>>>name2.islower()
False
>>>name1=“praveen“
>>>name1.islower()
True
Given string is not
lower case string
Given string is
lower case string
23. isupper() Method
isupper() method is used check string
contains all letters upper case or not, it returns
true or false result.
>>>name2=“Praveen”
>>>name2.isupper()
False
>>>name1=“PRAVEEN“
>>>name1.isupper()
True
Given string is not
upper case string
Given string is
upper case string
24. isspace() Method
isspace() method is used check string
contains space only or not.
>>>name2=“ ”
>>>name2.isspace()
True
>>>name1=“PRAVEEN M J“
>>>name1.isspace()
False
Given string
contains space only
Given string not
containing space only
25. find() Methods
find() method is used to find a
particular string (substring) in a given
string.
>>>name=“Classic”
>>>name.find(“s”)
3
the first position
where “s” appears
String object String Method Method Argument
26. str() Method
str() method is used convert non
string data into string type.
>>>str(576)
‘576’
576 is number converted to string
27 len() Method
len() method is used get a length of
string.
>>>len(“Praveen”)
7
Gives the string length
28 max() Method
max() method is used get a max
alphabet of string.
>>>max(“Praveen”)
v
Gives max character
29 min() Method
min() method is used get a max
alphabet of string.
>>>min(“Praveen”)
P
Gives min character P because it has
ASCII Value 65
30 split() Method
split() method is used split a string.
Split in to several words or substrings
30 split() Method
split() method is used split a string
according to delimiter.
Split in to several words or substrings
according to delimiter
31 index() Method
Same as find(), but raises an
exception if str not found.
>>> name="Sainik“
>>> name.index("a",3,5)
ValueError: substring not found
>>> name.index("a",1,5)
1 Value error
Character found, returning the position
Character Methods
32. ord() Method
ord() method is used get a ASCII
value for a character.
>>ord(“a”)
97
97 is the ASCII value for character ‘a’
33. chr() Method
char() method is used get a
character for an ASCII value.
>>chr(97)
‘a’
‘a’ ASCII value is 97
Detailed description of Strings in Python
Thank You

More Related Content

PPTX
Chapter 11 Strings.pptx
PPTX
Chapter 11 Strings and methods [Autosaved].pptx
PPTX
Chapter 14 strings
PDF
Strings brief introduction in python.pdf
PPTX
Java String Handling
PDF
Module 3 - Regular Expressions, Dictionaries.pdf
PPTX
Day5 String python language for btech.pptx
PDF
stringsinpython-181122100212.pdf
Chapter 11 Strings.pptx
Chapter 11 Strings and methods [Autosaved].pptx
Chapter 14 strings
Strings brief introduction in python.pdf
Java String Handling
Module 3 - Regular Expressions, Dictionaries.pdf
Day5 String python language for btech.pptx
stringsinpython-181122100212.pdf

Similar to Detailed description of Strings in Python (20)

PDF
Python programming : Strings
PDF
Strings in python
PPTX
Python Workshop
PDF
regular-expression.pdf
PPTX
Python Programming-UNIT-II - Strings.pptx
DOCX
Array assignment
PPTX
unit-4 regular expression.pptx
PPTX
String functions
PPTX
Python String Revisited.pptx
PPTX
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
PDF
Python data handling
PPTX
Python Strings.pptx
PPTX
PRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptx
PPT
Chapter 9 - Characters and Strings
PDF
Python Strings Methods
PPT
Java căn bản - Chapter9
PPTX
Strings.pptx
PPTX
String.pptxihugyftgrfxdf bnjklihugyfthfgxvhbjihugyfthcgxcgvjhbkipoihougyfctgf...
PPTX
stringggg.pptxtujd7ttttttttttttttttttttttttttttttttttt
PPTX
Basic python part 1
Python programming : Strings
Strings in python
Python Workshop
regular-expression.pdf
Python Programming-UNIT-II - Strings.pptx
Array assignment
unit-4 regular expression.pptx
String functions
Python String Revisited.pptx
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
Python data handling
Python Strings.pptx
PRESENTATION ON STRING, LISTS AND TUPLES IN PYTHON.pptx
Chapter 9 - Characters and Strings
Python Strings Methods
Java căn bản - Chapter9
Strings.pptx
String.pptxihugyftgrfxdf bnjklihugyfthfgxvhbjihugyfthcgxcgvjhbkipoihougyfctgf...
stringggg.pptxtujd7ttttttttttttttttttttttttttttttttttt
Basic python part 1
Ad

Recently uploaded (20)

PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
top salesforce developer skills in 2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Introduction to Artificial Intelligence
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
medical staffing services at VALiNTRY
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Transform Your Business with a Software ERP System
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Nekopoi APK 2025 free lastest update
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Design an Analysis of Algorithms I-SECS-1021-03
How Creative Agencies Leverage Project Management Software.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
Wondershare Filmora 15 Crack With Activation Key [2025
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Upgrade and Innovation Strategies for SAP ERP Customers
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Odoo Companies in India – Driving Business Transformation.pdf
top salesforce developer skills in 2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
CHAPTER 2 - PM Management and IT Context
Introduction to Artificial Intelligence
Reimagine Home Health with the Power of Agentic AI​
medical staffing services at VALiNTRY
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Transform Your Business with a Software ERP System
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Nekopoi APK 2025 free lastest update
Ad

Detailed description of Strings in Python

  • 2. Like many other popular programming languages, strings in Python are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string. STRINGS
  • 3. How to change or delete a string?
  • 4. Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. We can simply reassign different strings to the same name. >>> my_string = 'programiz' >>> my_string[5] = 'a' TypeError: 'str' object does not support item assignment. How to change or delete a string?
  • 5. How to create a string in Python?
  • 6. How to create a string in Python? Strings can be created by enclosing characters inside a single quote or double quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings. STRINGS
  • 8. REPRESENTATION OF STRING >>> s = “Hello Python” This is how Python would index the string: Forward Indexing Backward Indexing
  • 10. STRINGS - Example Output Next Slide...
  • 12. How to access characters in a string?
  • 13. How to access characters in a string? We can access individual characters using indexing and a range of characters using slicing. Index starts from 0. Trying to access a character out of index range will raise an IndexError. The index must be an integer. We can't use float or other types, this will result into TypeError. STRINGS
  • 14. How to access characters in a string? Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on. We can access a range of items in a string by using the slicing operator (colon). STRINGS
  • 18. SLICING STRINGS EXAMPLES For example: >>>“Program”[3:5] will result in: ‘gr ’ >>>“Program”[3:6] will yield: ‘gra’ >>>p = “Program” >>>p [:4] ‘Prog’ >>>p = “Program” >>>p [4:] ‘ram’ >>>p = “Program” >>>p [3:6] ‘gra’
  • 22. More Functionality of String >>> len(“Sainik School”) 13 >>>print(“Sainik” + “School”) Sainik School >>>print(“A” * 4 ) AAAA >>>”sa” in “sainik” True >>>”pr” in “computer” False Finding Length of string String Concatenation String Repeat Substring Tests
  • 23. More Functionality of String >>> name1="computer" >>> name2=name1[3:5] >>>name2 pu String Slicing
  • 25. String Methods In Python, a method is a function that is defined with respect to a particular object. Syntax: object.method(arguments) For Example: >>>name=“Classic” >>>name.find(“s”) 3 the first position where “s” appears String object String Method Method Argument
  • 27. 1. capitalize() Method Capitalizes first letter of string First letter capitalization
  • 28. 2. lstrip() & 3. rstrip() Methods lstrip() method is used to remove left padded spaces in a given string >>>name1=“ a “ >>>name1.lstrip() ‘a ‘ rstrip() method is used to remove right padded spaces in a given string >>>name1.rstrip() ‘ a’ Removing left spaces Removing right spaces
  • 29. 4. strip() Method strip() method is used to remove left and right padded spaces in a given string >>>name1=“ a “ >>>name1.strip() ‘a‘ Removing left and right spaces for a given string
  • 30. 5. lower() Method lower() method is used to convert given string in to lower case. >>>name1=“ SAINIK “ >>>name1.lower() sainik Converting in to lower case
  • 31. 6. upper() Method upper() method is used to convert given string in to upper case. >>>name1=“ sainik “ >>>name1.upper() SAINIK Converting in to upper case
  • 32. 7. title() Method title() method is used to convert given string in to title case. Every first chatacter of word of a given string is converted to title case. >>>name1=“ praveen murigeppa jigajinni“ >>>name1.title() Praveen Murigeppa Jigajinni In every word first letter is capitalized
  • 33. 8. swapcase() Method swapcase() method is toggle the case. Meaining upper to lower and lower to upper case. >>>name1=“ PrAvEeN“ >>>name1.swapcase() pRaVeEn Every character case is changed
  • 34. 9. ljust() Method ljust() method is used to add spaces to the left side of the given string >>>name1=“ Jigajinni“ >>>name1.ljust(15) ‘Jigajinni ’ Left side padded with spaces Note: string length is 9 and 6 spaces added to the left side of string
  • 35. 10. rjust() Method rjust() method is used to add spaces to the right side of the given string >>>name1=“ Jigajinni“ >>>name1.rjust(15) ‘ Jigajinni’ Right side padded with spaces Note: string length is 9 and 6 spaces added to the right side of string
  • 36. 11. center(width, fillchar) Method The method center() returns centered in a string of length width. Padding is done using the specified fillchar. Default filler is a space. Centered string
  • 37. 12. zfill() Method zfill() method is used to fill the zero to a given string >>>name1=“ 123“ >>>name1.zfill(15) ‘00123 ’ Filling Zeros
  • 38. 13. find() Method find() method is used to find a perticular character or string in a given string. >>> name1="praveen" >>> name1.find("e") 4 e is present at 4th location (first appearance) in a given string
  • 39. 14. count() Method count() method is used to the number of times character or string appears in a given string. >>>name1=“praveen“ >>>name1.count(“e”) 2 2 times e appears in a given string
  • 40. 15. stratswith() Method startswith() method is used check string start with particular string or not >>>name1=“praveen“ >>>name1.startswith(“a”) False Given string not starting with “a”
  • 41. 16. endswith() Method endswith() method is used check string ends with particular string or not >>>name1=“praveen“ >>>name1.endswith(“en”) True Given string ends with “en”
  • 42. 17. isdigit() Method isdigit() method is used check string is digit (number) or not and returns Boolean value true or false. >>>name2=“123” >>>name2.isdigit() True >>name1=“123keyboard“ >>name1.isdigit() False Given string not number so false Given string is number
  • 43. 18. isnumeric() Method isnumeric() is similar to isdigit() method and is used check string is digit (number) or not and returns Boolean value true or false. >>>name2=“123” >>>name2.isnumeric() True >>name1=“123keyboard“ >>name1.isnumeric() False Given string not number so false Given string is number
  • 44. 19. isdecimal() Method isnumeric(),isdigit() and isdecimal() methods are used to check string is digit (number) or not and returns Boolean value true or false. >>>name2=“123” >>>name2.decimal() True >>name1=“123keyboard“ >>name1.isnumeric() False Given string not number so false Given string is number
  • 45. 20. isalpha() Method isalpha() method is used check string is digit or not and returns Boolean value true or false. >>>name2=“123” >>>name2.isalpha() False >>name1=“123computer“ >>name1.isalpha() False >>>name3=“Keynoard” >>>Name3.isaplpha() True Given string not string it contains digits Given string does not contain string It’s a string
  • 46. 21. isalnum() Method isalnum() method is used check string is alpha numeric string or not. >>>name2=“123” >>>name2.isalnum() True >>>name1=“123computer“ >>>name1.isalnum() True >>>name3=“Praveen” >>>name3.isalnum() True Given string is alpha numeric
  • 47. 22. islower() Method islower() method is used check string contains all lower case letters or not, it returns true or false result. >>>name2=“Praveen” >>>name2.islower() False >>>name1=“praveen“ >>>name1.islower() True Given string is not lower case string Given string is lower case string
  • 48. 23. isupper() Method isupper() method is used check string contains all letters upper case or not, it returns true or false result. >>>name2=“Praveen” >>>name2.isupper() False >>>name1=“PRAVEEN“ >>>name1.isupper() True Given string is not upper case string Given string is upper case string
  • 49. 24. isspace() Method isspace() method is used check string contains space only or not. >>>name2=“ ” >>>name2.isspace() True >>>name1=“PRAVEEN M J“ >>>name1.isspace() False Given string contains space only Given string not containing space only
  • 50. 25. find() Methods find() method is used to find a particular string (substring) in a given string. >>>name=“Classic” >>>name.find(“s”) 3 the first position where “s” appears String object String Method Method Argument
  • 51. 26. str() Method str() method is used convert non string data into string type. >>>str(576) ‘576’ 576 is number converted to string
  • 52. 27 len() Method len() method is used get a length of string. >>>len(“Praveen”) 7 Gives the string length
  • 53. 28 max() Method max() method is used get a max alphabet of string. >>>max(“Praveen”) v Gives max character
  • 54. 29 min() Method min() method is used get a max alphabet of string. >>>min(“Praveen”) P Gives min character P because it has ASCII Value 65
  • 55. 30 split() Method split() method is used split a string. Split in to several words or substrings
  • 56. 30 split() Method split() method is used split a string according to delimiter. Split in to several words or substrings according to delimiter
  • 57. 31 index() Method Same as find(), but raises an exception if str not found. >>> name="Sainik“ >>> name.index("a",3,5) ValueError: substring not found >>> name.index("a",1,5) 1 Value error Character found, returning the position
  • 59. 32. ord() Method ord() method is used get a ASCII value for a character. >>ord(“a”) 97 97 is the ASCII value for character ‘a’
  • 60. 33. chr() Method char() method is used get a character for an ASCII value. >>chr(97) ‘a’ ‘a’ ASCII value is 97