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

More Related Content

Similar to 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 Python
Sujith 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.pdf
MegMeg17
 
Introduction to R for beginners
Introduction to R for beginnersIntroduction to R for beginners
Introduction to R for beginners
Abishek Purushothaman
 
Python ppt_118.pptx
Python ppt_118.pptxPython ppt_118.pptx
Python ppt_118.pptx
MadhuriAnaparthy
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
Max 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 data
SukhpreetSingh519414
 
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 begginners
Abishek Purushothaman
 
Python Strings Format
Python Strings FormatPython Strings Format
Python Strings Format
Mr Examples
 
Chapter - 2.pptx
Chapter - 2.pptxChapter - 2.pptx
Chapter - 2.pptx
MikialeTesfamariam
 
Finaal application on regular expression
Finaal application on regular expressionFinaal application on regular expression
Finaal application on regular expression
Gagan019
 
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
 
grep.1.pdf
grep.1.pdfgrep.1.pdf
grep.1.pdf
Saravana Kumar
 
grep.1.pdf
grep.1.pdfgrep.1.pdf
grep.1.pdf
Saravana Kumar
 

Similar to 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
 

More from jaychoudhary37

Reflector Antennas Design and mathematical expression_1.pptx
Reflector Antennas Design and mathematical expression_1.pptxReflector Antennas Design and mathematical expression_1.pptx
Reflector Antennas Design and mathematical expression_1.pptx
jaychoudhary37
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
jaychoudhary37
 
Metamaterial Devices and types of antenna
Metamaterial Devices and types of antennaMetamaterial Devices and types of antenna
Metamaterial Devices and types of antenna
jaychoudhary37
 
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
jaychoudhary37
 
introduction to Microcontrollers 8051.ppt
introduction to Microcontrollers 8051.pptintroduction to Microcontrollers 8051.ppt
introduction to Microcontrollers 8051.ppt
jaychoudhary37
 
Metamaterial Devices and There description
Metamaterial Devices and There descriptionMetamaterial Devices and There description
Metamaterial Devices and There description
jaychoudhary37
 
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.pdf
jaychoudhary37
 

More from jaychoudhary37 (9)

Reflector Antennas Design and mathematical expression_1.pptx
Reflector Antennas Design and mathematical expression_1.pptxReflector Antennas Design and mathematical expression_1.pptx
Reflector Antennas Design and mathematical expression_1.pptx
 
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
 

Recently uploaded

Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
Kamal Acharya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 

Recently uploaded (20)

Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.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.