SlideShare une entreprise Scribd logo
1  sur  34
1
Python Strings Revisited
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Strings
2
Python treats strings as contiguous series of characters delimited by single, double or even triple quotes. Python
has a built-in string class named "str" that has many useful features. We can simultaneously declare and define a
string by creating a variable of string type. This can be done in several ways which are as follows:
name = "India" graduate = 'N' country = name nationality = str("Indian")
Indexing: Individual characters in a string are accessed using the subscript ([ ]) operator. The expression in
brackets is called an index. The index specifies a member of an ordered set and in this case it specifies the
character we want to access from the given set of characters in the string.
The index of the first character is 0 and that of the last character is n-1 where n is the number of characters in the
string. If you try to exceed the bounds (below 0 or above n-1), then an error is raised.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Strings
3
Traversing a String: A string can be traversed by accessing character(s) from one index to another. For example,
the following program uses indexing to traverse a string from first character to the last.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Example:
Concatenating, Appending and Multiplying Strings
4
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Examples:
Strings are Immutable
5
Python strings are immutable which means that once created they cannot be changed. Whenever you try to modify
an existing string variable, a new string is created.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Example:
String Formatting Operator
6
The % operator takes a format string on the left (that has %d, %s, etc) and the corresponding values in a tuple (will
be discussed in subsequent chapter) on the right. The format operator, % allow users to construct strings, replacing
parts of the strings with the data stored in variables. The syntax for the string formatting operation is:
"<Format>" % (<Values>)
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Example:
Built-in String Methods and Functions
7
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Built-in String Methods and Functions
8
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Built-in String Methods and Functions
9
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Built-in String Methods and Functions
10
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Slice Operation
11
A substring of a string is called a slice. The slice operation is used to refer to sub-parts of sequences and strings.
You can take subset of string from original string by using [ ] operator also known as slicing operator.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Examples:
Specifying Stride while Slicing Strings
12
In the slice operation, you can specify a third argument as the stride, which refers to the number of characters to
move forward after the first character is retrieved from the string. By default the value of stride is 1, so in all the
above examples where he had not specified the stride, it used the value of 1 which means that every character
between two index numbers is retrieved.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Examples:
ord() and chr() Functions
13
ord() function returns the ASCII code of the character and chr() function returns character represented by a ASCII
number.
in and not in Operators
in and not in operators can be used with strings to determine whether a string is present in another string.
Therefore, the in and not in operator are also known as membership operators.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Examples:
Examples:
Comparing Strings
14
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Iterating String
15
String is a sequence type (sequence of characters). You can iterate through the string using for loop.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Examples:
The String Module
16
The string module consist of a number of useful constants, classes and functions (some of which are deprecated).
These functions are used to manipulate strings.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Examples:
Working with Constants in String Module
17
You can use the constants defined in the string module along with the find function to classify characters. For
example, if find(lowercase, ch) returns a value except -1, then it means that ch must be a lowercase character. An
alternate way to do the same job is to use the in operator or even the comparison operation.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Examples:
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 18
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 19
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 20
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 21
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 22
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 23
Regular Expressions
24
Regular Expressions are a powerful tool for various kinds of string manipulation. These are basically a special text
string that is used for describing a search pattern to extract information from text such as code, files, log,
spreadsheets, or even documents.
Regular expressions are a domain specific language (DSL) that is present as a library in most of the modern
programming languages, besides Python. A regular expression is a special sequence of characters that helps to
match or find strings in another string. In Python, regular expressions can be accessed using the re module which
comes as a part of the Standard Library
The Match Function
As the name suggest, the match() function matches a pattern to string with optional flags. The syntax of match()
function is,
re.match (pattern, string, flags=0)
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Match() Function
25
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
EXAMPLE- Match() Function
26
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Examples:
Different
values of
Flags
The search Function
27
The search() function in the re module searches for a pattern anywhere in the string. Its syntax of can be given as,
re.search(pattern, string, flags=0)
The syntax is similar to the match() function. The function searches for first occurrence of pattern within a string
with optional flags. If the search is successful, a match object is returned and None otherwise.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Example:
The sub() Function
28
The sub() function in the re module can be used to search a pattern in the string and replace it with another
pattern. The syntax of sub() function can be given as, re.sub(pattern, repl, string, max=0)
According to the syntax, the sub() function replaces all occurrences of the pattern in string with repl, substituting
all occurrences unless any max value is provided. This method returns modified string.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Example:
The findall() and finditer() Function
29
The findall() function is used to search a string and returns a list of matches of the pattern in the string. If no
match is found, then the returned list is empty. The syntax of match() function can be given as,
matchList = re.findall(pattern, input_str, flags=0)
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Examples:
Flag Options
30
The search(), findall() and match() functions of the module take options to modify the behavior of the pattern
match. Some of these flags are:
re.I or re.IGNORECASE — Ignores case of characters, so "Match", "MATCH", "mAtCh", etc are all same
re.S or re.DOTALL — Enables dot (.) to match newline. By default, dot matches any character other than the newline
character.
re.M or re.MULTILINE — Makes the ^ and $ to match the start and end of each line. That is, it matches even after
and before line breaks in the string. By default, ^ and $ matches the start and end of the whole string.
re.L or re.LOCALE- Makes the flag w to match all characters that are considered letters in the given current locale
settings.
re.U or re.UNICODE- Treats all letters from all scripts as word characters.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Metacharacters in Regular Expression
31
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Metacharacters in Regular Expression
32
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Character Classes
33
When we put the characters to be matched inside square brackets, we call it a character class. For example,
[aeiou] defines a character class that has a vowel character.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Examples:
Groups
34
A group is created by surrounding a part of the regular expression with parentheses. You can even give group as
an argument to the metacharacters such as * and ?.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Example:
The content of groups in a match can be accessed by using the group() function. For example,
• group(0) or group() returns the whole match.
• group(n), where n is greater than 0, returns the nth group from the left.
• group() returns all groups up from 1.

Contenu connexe

Similaire à FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_06-Dec-2022_Chapter_6.pptx

Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressionsAcácio Oliveira
 
Module 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfModule 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfMegMeg17
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 
ppt notes python language operators and data
ppt notes python language operators and datappt notes python language operators and data
ppt notes python language operators and dataSukhpreetSingh519414
 
C presentation! BATRA COMPUTER CENTRE
C presentation! BATRA  COMPUTER  CENTRE C presentation! BATRA  COMPUTER  CENTRE
C presentation! BATRA COMPUTER CENTRE jatin batra
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginnersAbishek Purushothaman
 
Python Strings Format
Python Strings FormatPython Strings Format
Python Strings FormatMr Examples
 
Finaal application on regular expression
Finaal application on regular expressionFinaal application on regular expression
Finaal application on regular expressionGagan019
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arraysphanleson
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYRajeshkumar Reddy
 

Similaire à FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_06-Dec-2022_Chapter_6.pptx (20)

Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Perl_Part4
Perl_Part4Perl_Part4
Perl_Part4
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
 
C# String
C# StringC# String
C# String
 
Module 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfModule 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdf
 
Introduction to R for beginners
Introduction to R for beginnersIntroduction to R for beginners
Introduction to R for beginners
 
Python ppt_118.pptx
Python ppt_118.pptxPython ppt_118.pptx
Python ppt_118.pptx
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
ppt notes python language operators and data
ppt notes python language operators and datappt notes python language operators and data
ppt notes python language operators and data
 
C presentation! BATRA COMPUTER CENTRE
C presentation! BATRA  COMPUTER  CENTRE C presentation! BATRA  COMPUTER  CENTRE
C presentation! BATRA COMPUTER CENTRE
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
 
Python Strings Format
Python Strings FormatPython Strings Format
Python Strings Format
 
Chapter - 2.pptx
Chapter - 2.pptxChapter - 2.pptx
Chapter - 2.pptx
 
String
StringString
String
 
Finaal application on regular expression
Finaal application on regular expressionFinaal application on regular expression
Finaal application on regular expression
 
LectureNotes-04-DSA
LectureNotes-04-DSALectureNotes-04-DSA
LectureNotes-04-DSA
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
 
grep.1.pdf
grep.1.pdfgrep.1.pdf
grep.1.pdf
 
grep.1.pdf
grep.1.pdfgrep.1.pdf
grep.1.pdf
 

Plus de jaychoudhary37

microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
Metamaterial Devices and types of antenna
Metamaterial Devices and types of antennaMetamaterial Devices and types of antenna
Metamaterial Devices and types of antennajaychoudhary37
 
smart antenna and types of smart antenna7453684.ppt
smart antenna and types of smart antenna7453684.pptsmart antenna and types of smart antenna7453684.ppt
smart antenna and types of smart antenna7453684.pptjaychoudhary37
 
introduction to Microcontrollers 8051.ppt
introduction to Microcontrollers 8051.pptintroduction to Microcontrollers 8051.ppt
introduction to Microcontrollers 8051.pptjaychoudhary37
 
Metamaterial Devices and There description
Metamaterial Devices and There descriptionMetamaterial Devices and There description
Metamaterial Devices and There descriptionjaychoudhary37
 
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...jaychoudhary37
 
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...jaychoudhary37
 
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdf
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdfELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdf
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdfjaychoudhary37
 

Plus de jaychoudhary37 (8)

microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
Metamaterial Devices and types of antenna
Metamaterial Devices and types of antennaMetamaterial Devices and types of antenna
Metamaterial Devices and types of antenna
 
smart antenna and types of smart antenna7453684.ppt
smart antenna and types of smart antenna7453684.pptsmart antenna and types of smart antenna7453684.ppt
smart antenna and types of smart antenna7453684.ppt
 
introduction to Microcontrollers 8051.ppt
introduction to Microcontrollers 8051.pptintroduction to Microcontrollers 8051.ppt
introduction to Microcontrollers 8051.ppt
 
Metamaterial Devices and There description
Metamaterial Devices and There descriptionMetamaterial Devices and There description
Metamaterial Devices and There description
 
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
 
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...
 
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdf
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdfELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdf
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdf
 

Dernier

Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxVelmuruganTECE
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsSachinPawar510423
 
Industrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESIndustrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESNarmatha D
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadaditya806802
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 

Dernier (20)

Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptx
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documents
 
Industrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIESIndustrial Safety Unit-I SAFETY TERMINOLOGIES
Industrial Safety Unit-I SAFETY TERMINOLOGIES
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasad
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 

FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_06-Dec-2022_Chapter_6.pptx

  • 1. 1 Python Strings Revisited © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 2. Strings 2 Python treats strings as contiguous series of characters delimited by single, double or even triple quotes. Python has a built-in string class named "str" that has many useful features. We can simultaneously declare and define a string by creating a variable of string type. This can be done in several ways which are as follows: name = "India" graduate = 'N' country = name nationality = str("Indian") Indexing: Individual characters in a string are accessed using the subscript ([ ]) operator. The expression in brackets is called an index. The index specifies a member of an ordered set and in this case it specifies the character we want to access from the given set of characters in the string. The index of the first character is 0 and that of the last character is n-1 where n is the number of characters in the string. If you try to exceed the bounds (below 0 or above n-1), then an error is raised. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 3. Strings 3 Traversing a String: A string can be traversed by accessing character(s) from one index to another. For example, the following program uses indexing to traverse a string from first character to the last. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Example:
  • 4. Concatenating, Appending and Multiplying Strings 4 © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Examples:
  • 5. Strings are Immutable 5 Python strings are immutable which means that once created they cannot be changed. Whenever you try to modify an existing string variable, a new string is created. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Example:
  • 6. String Formatting Operator 6 The % operator takes a format string on the left (that has %d, %s, etc) and the corresponding values in a tuple (will be discussed in subsequent chapter) on the right. The format operator, % allow users to construct strings, replacing parts of the strings with the data stored in variables. The syntax for the string formatting operation is: "<Format>" % (<Values>) © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Example:
  • 7. Built-in String Methods and Functions 7 © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 8. Built-in String Methods and Functions 8 © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 9. Built-in String Methods and Functions 9 © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 10. Built-in String Methods and Functions 10 © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 11. Slice Operation 11 A substring of a string is called a slice. The slice operation is used to refer to sub-parts of sequences and strings. You can take subset of string from original string by using [ ] operator also known as slicing operator. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Examples:
  • 12. Specifying Stride while Slicing Strings 12 In the slice operation, you can specify a third argument as the stride, which refers to the number of characters to move forward after the first character is retrieved from the string. By default the value of stride is 1, so in all the above examples where he had not specified the stride, it used the value of 1 which means that every character between two index numbers is retrieved. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Examples:
  • 13. ord() and chr() Functions 13 ord() function returns the ASCII code of the character and chr() function returns character represented by a ASCII number. in and not in Operators in and not in operators can be used with strings to determine whether a string is present in another string. Therefore, the in and not in operator are also known as membership operators. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Examples: Examples:
  • 14. Comparing Strings 14 © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 15. Iterating String 15 String is a sequence type (sequence of characters). You can iterate through the string using for loop. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Examples:
  • 16. The String Module 16 The string module consist of a number of useful constants, classes and functions (some of which are deprecated). These functions are used to manipulate strings. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Examples:
  • 17. Working with Constants in String Module 17 You can use the constants defined in the string module along with the find function to classify characters. For example, if find(lowercase, ch) returns a value except -1, then it means that ch must be a lowercase character. An alternate way to do the same job is to use the in operator or even the comparison operation. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Examples:
  • 18. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 18
  • 19. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 19
  • 20. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 20
  • 21. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 21
  • 22. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 22
  • 23. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 23
  • 24. Regular Expressions 24 Regular Expressions are a powerful tool for various kinds of string manipulation. These are basically a special text string that is used for describing a search pattern to extract information from text such as code, files, log, spreadsheets, or even documents. Regular expressions are a domain specific language (DSL) that is present as a library in most of the modern programming languages, besides Python. A regular expression is a special sequence of characters that helps to match or find strings in another string. In Python, regular expressions can be accessed using the re module which comes as a part of the Standard Library The Match Function As the name suggest, the match() function matches a pattern to string with optional flags. The syntax of match() function is, re.match (pattern, string, flags=0) © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 25. Match() Function 25 © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 26. EXAMPLE- Match() Function 26 © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Examples: Different values of Flags
  • 27. The search Function 27 The search() function in the re module searches for a pattern anywhere in the string. Its syntax of can be given as, re.search(pattern, string, flags=0) The syntax is similar to the match() function. The function searches for first occurrence of pattern within a string with optional flags. If the search is successful, a match object is returned and None otherwise. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Example:
  • 28. The sub() Function 28 The sub() function in the re module can be used to search a pattern in the string and replace it with another pattern. The syntax of sub() function can be given as, re.sub(pattern, repl, string, max=0) According to the syntax, the sub() function replaces all occurrences of the pattern in string with repl, substituting all occurrences unless any max value is provided. This method returns modified string. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Example:
  • 29. The findall() and finditer() Function 29 The findall() function is used to search a string and returns a list of matches of the pattern in the string. If no match is found, then the returned list is empty. The syntax of match() function can be given as, matchList = re.findall(pattern, input_str, flags=0) © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Examples:
  • 30. Flag Options 30 The search(), findall() and match() functions of the module take options to modify the behavior of the pattern match. Some of these flags are: re.I or re.IGNORECASE — Ignores case of characters, so "Match", "MATCH", "mAtCh", etc are all same re.S or re.DOTALL — Enables dot (.) to match newline. By default, dot matches any character other than the newline character. re.M or re.MULTILINE — Makes the ^ and $ to match the start and end of each line. That is, it matches even after and before line breaks in the string. By default, ^ and $ matches the start and end of the whole string. re.L or re.LOCALE- Makes the flag w to match all characters that are considered letters in the given current locale settings. re.U or re.UNICODE- Treats all letters from all scripts as word characters. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 31. Metacharacters in Regular Expression 31 © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 32. Metacharacters in Regular Expression 32 © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
  • 33. Character Classes 33 When we put the characters to be matched inside square brackets, we call it a character class. For example, [aeiou] defines a character class that has a vowel character. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Examples:
  • 34. Groups 34 A group is created by surrounding a part of the regular expression with parentheses. You can even give group as an argument to the metacharacters such as * and ?. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. Example: The content of groups in a match can be accessed by using the group() function. For example, • group(0) or group() returns the whole match. • group(n), where n is greater than 0, returns the nth group from the left. • group() returns all groups up from 1.