SlideShare a Scribd company logo
LECTURE – STRINGS
BY: ABHISHEK BHARDWAJ
PGT- COMPUTER SCIENCE
Strings in Python
Strings in Python
String Printing
String Operators
String Operators
Character ASCII Code
A 65
B 66
a 97
b 98
String Slicing
String Slicing
D
String Functions
Length of the String & Printing String Character by
Character (Traversing a String using Loop)
CODE SNIPPET
OUTPUT
CODE SNIPPET
OUTPUT
Capitalize String’s First Character
CODE SNIPPET
OUTPUT
OUTPUT
From the above Output, it is clear that capitalize() only
changes the String shown in output, but it does not
change the Original String.
Program to Find Reverse a String
(Method 1 : String Slicing)
CODE SNIPPET
OUTPUT
Start from
Backward Index
i. e. -1 Index
End not defined means
default till end i. e. 0 Index
Step Value
i.e. -1
Decrease by 1 in each step
Program to Find Reverse a String
(Method 2 : Programming Capabilities)
CODE SNIPPET
OUTPUT
Length of String
-1 means last
character of
string
End is defined as -1 because it
is taken as one less than
actual value i.e. 0
Step Value
i.e. -1
Program to Count Total Vowels and Consonants in a
String
CODE SNIPPET
Start from 0
Index
Step Value by default will
increase by 1 in each step
OUTPUT
End is defined as end of
string index
String Functions
Program to Find Lowest Index in the String where
Substring is found
CODE SNIPPET
Start from 0
Index
End Index one less from
the string length
OUTPUT
Substring to Find
Program to Check whether given String is Palindrome
or Not?
CODE SNIPPET
Step Value
i.e. -1
Decrease by 1 in each step
OUTPUT
Start from
Backward Index
i. e. -1 Index
End not defined means
default till end i. e. 0 Index
String Functions
String Functions
String Functions
String Functions
title() : Make the first letter in each word upper
case.
CODE SNIPPET OUTPUT
lower() : Lower case the string
CODE SNIPPET OUTPUT
String Functions
upper() : Upper case the string.
CODE SNIPPET OUTPUT
count() : Return the number of times a specified
value appears in the string.
CODE SNIPPET OUTPUT
String Functions
index() : The index() method finds the first occurrence of
the specified value. The index() method raises an
exception if the value is not found .
CODE SNIPPET OUTPUT
String Functions
endswith() : The endswith() method returns True if the
string ends with the specified value, otherwise False.
Syntax : string.endswith(value, start, end).
CODE SNIPPET OUTPUT
String Functions
startswith() : The startswith() method returns True if the
string starts with the specified value, otherwise False.
Syntax : string.startswith(value, start, end)
CODE SNIPPET OUTPUT
String Functions
isalpha() : The isalpha() method returns True if all the
characters are alphabet letters (a-z).
Syntax : string.isalpha()
CODE SNIPPET OUTPUT
String Functions
islower() : The islower() method returns True if all the
characters in the text are in lower case, otherwise
False. Numbers, symbols and spaces are not checked.
Syntax : string.islower()
CODE SNIPPET OUTPUT
String Functions
isupper() : The isupper() method returns True if all the
characters in the text are in upper case, otherwise
False. Numbers, symbols and spaces are not checked.
Syntax : string.isupper()
CODE SNIPPET OUTPUT
String Functions
lstrip() : The lstrip() method removes any leading
characters. (space is the default leading character to
remove)
Syntax : string.lstrip(characters)
CODE SNIPPET OUTPUT
String Functions
rstrip() : The rstrip() method removes any trailing
characters (characters at the end a string), space is the
default trailing character to remove.
Syntax : string.rstrip(characters)
CODE SNIPPET OUTPUT
String Functions
strip() : The strip() method removes any leading (spaces at
the beginning) and trailing (spaces at the end) characters.
(space is the default leading character to remove)
Syntax : string.strip(characters)
CODE SNIPPET OUTPUT
String Functions
replace() : The replace() method replaces a specified
phrase with another specified phrase.
Syntax : string.replace(oldvalue, newvalue, count)
CODE SNIPPET
OUTPUT
String Functions
join() : The join() method takes all items in an iterable and
joins them into one string. A string must be specified as
the separator.
Syntax : string.join(iterable)  Any iterable object where
all the returned values are strings
CODE SNIPPET OUTPUT
CODE SNIPPET OUTPUT
When using a dictionary as an iterable, the returned values are the keys, not the values.
String Functions
partition() : The partition() method searches for a specified string,
and splits the string into a tuple containing three elements.
 The first element contains the part before the specified string.
 The second element contains the specified string.
 The third element contains the part after the string.
Syntax : string.partition(value)
CODE SNIPPET OUTPUT
# This method searches for the first occurrence of the specified string.
# If the specified value is not found, the partition() method returns a tuple containing:
1 - the whole string, 2 - an empty string, 3 - an empty string.
String Functions
split() : The split() method splits a string into a list. You can
specify the separator (default separator is any whitespace).
Syntax : string.split(separator, maxsplit)
CODE SNIPPET
OUTPUT
separator - Optional. Specifies the separator to use when splitting the string. By
default any whitespace is a separator.
maxsplit - Optional. Specifies how many splits to do. Default value is -1, which is
"all occurrences"
String Functions - split()
CODE SNIPPET
OUTPUT
separator - Optional. Specifies the separator to use when splitting the string. By
default any whitespace is a separator.
maxsplit - Optional. Specifies how many splits to do. Default value is -1, which is
"all occurrences"

More Related Content

Similar to String.pdf

Virtusa questions placement preparation guide
Virtusa questions placement preparation guideVirtusa questions placement preparation guide
Virtusa questions placement preparation guide
ThalaAjith33
 
ObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docxObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docx
vannagoforth
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
phanleson
 

Similar to String.pdf (20)

Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Virtusa questions placement preparation guide
Virtusa questions placement preparation guideVirtusa questions placement preparation guide
Virtusa questions placement preparation guide
 
Strings part2
Strings part2Strings part2
Strings part2
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
ObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docxObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docx
 
Python data handling
Python data handlingPython data handling
Python data handling
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
Introduction To Programming with Python-3
Introduction To Programming with Python-3Introduction To Programming with Python-3
Introduction To Programming with Python-3
 
Team 1
Team 1Team 1
Team 1
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Python reference
Python referencePython reference
Python reference
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
M C6java7
M C6java7M C6java7
M C6java7
 
Numpy string functions
Numpy string functionsNumpy string functions
Numpy string functions
 
String notes
String notesString notes
String notes
 
STRINGS IN PYTHON
STRINGS IN PYTHONSTRINGS IN PYTHON
STRINGS IN PYTHON
 

Recently uploaded

Recently uploaded (20)

Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 

String.pdf

  • 1. LECTURE – STRINGS BY: ABHISHEK BHARDWAJ PGT- COMPUTER SCIENCE
  • 6. String Operators Character ASCII Code A 65 B 66 a 97 b 98
  • 10. Length of the String & Printing String Character by Character (Traversing a String using Loop) CODE SNIPPET OUTPUT CODE SNIPPET OUTPUT
  • 11. Capitalize String’s First Character CODE SNIPPET OUTPUT OUTPUT From the above Output, it is clear that capitalize() only changes the String shown in output, but it does not change the Original String.
  • 12. Program to Find Reverse a String (Method 1 : String Slicing) CODE SNIPPET OUTPUT Start from Backward Index i. e. -1 Index End not defined means default till end i. e. 0 Index Step Value i.e. -1 Decrease by 1 in each step
  • 13. Program to Find Reverse a String (Method 2 : Programming Capabilities) CODE SNIPPET OUTPUT Length of String -1 means last character of string End is defined as -1 because it is taken as one less than actual value i.e. 0 Step Value i.e. -1
  • 14. Program to Count Total Vowels and Consonants in a String CODE SNIPPET Start from 0 Index Step Value by default will increase by 1 in each step OUTPUT End is defined as end of string index
  • 16. Program to Find Lowest Index in the String where Substring is found CODE SNIPPET Start from 0 Index End Index one less from the string length OUTPUT Substring to Find
  • 17. Program to Check whether given String is Palindrome or Not? CODE SNIPPET Step Value i.e. -1 Decrease by 1 in each step OUTPUT Start from Backward Index i. e. -1 Index End not defined means default till end i. e. 0 Index
  • 21. String Functions title() : Make the first letter in each word upper case. CODE SNIPPET OUTPUT lower() : Lower case the string CODE SNIPPET OUTPUT
  • 22. String Functions upper() : Upper case the string. CODE SNIPPET OUTPUT count() : Return the number of times a specified value appears in the string. CODE SNIPPET OUTPUT
  • 23. String Functions index() : The index() method finds the first occurrence of the specified value. The index() method raises an exception if the value is not found . CODE SNIPPET OUTPUT
  • 24. String Functions endswith() : The endswith() method returns True if the string ends with the specified value, otherwise False. Syntax : string.endswith(value, start, end). CODE SNIPPET OUTPUT
  • 25. String Functions startswith() : The startswith() method returns True if the string starts with the specified value, otherwise False. Syntax : string.startswith(value, start, end) CODE SNIPPET OUTPUT
  • 26. String Functions isalpha() : The isalpha() method returns True if all the characters are alphabet letters (a-z). Syntax : string.isalpha() CODE SNIPPET OUTPUT
  • 27. String Functions islower() : The islower() method returns True if all the characters in the text are in lower case, otherwise False. Numbers, symbols and spaces are not checked. Syntax : string.islower() CODE SNIPPET OUTPUT
  • 28. String Functions isupper() : The isupper() method returns True if all the characters in the text are in upper case, otherwise False. Numbers, symbols and spaces are not checked. Syntax : string.isupper() CODE SNIPPET OUTPUT
  • 29. String Functions lstrip() : The lstrip() method removes any leading characters. (space is the default leading character to remove) Syntax : string.lstrip(characters) CODE SNIPPET OUTPUT
  • 30. String Functions rstrip() : The rstrip() method removes any trailing characters (characters at the end a string), space is the default trailing character to remove. Syntax : string.rstrip(characters) CODE SNIPPET OUTPUT
  • 31. String Functions strip() : The strip() method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters. (space is the default leading character to remove) Syntax : string.strip(characters) CODE SNIPPET OUTPUT
  • 32. String Functions replace() : The replace() method replaces a specified phrase with another specified phrase. Syntax : string.replace(oldvalue, newvalue, count) CODE SNIPPET OUTPUT
  • 33. String Functions join() : The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator. Syntax : string.join(iterable)  Any iterable object where all the returned values are strings CODE SNIPPET OUTPUT CODE SNIPPET OUTPUT When using a dictionary as an iterable, the returned values are the keys, not the values.
  • 34. String Functions partition() : The partition() method searches for a specified string, and splits the string into a tuple containing three elements.  The first element contains the part before the specified string.  The second element contains the specified string.  The third element contains the part after the string. Syntax : string.partition(value) CODE SNIPPET OUTPUT # This method searches for the first occurrence of the specified string. # If the specified value is not found, the partition() method returns a tuple containing: 1 - the whole string, 2 - an empty string, 3 - an empty string.
  • 35. String Functions split() : The split() method splits a string into a list. You can specify the separator (default separator is any whitespace). Syntax : string.split(separator, maxsplit) CODE SNIPPET OUTPUT separator - Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator. maxsplit - Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences"
  • 36. String Functions - split() CODE SNIPPET OUTPUT separator - Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator. maxsplit - Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences"