0% found this document useful (0 votes)
38 views46 pages

Unit Iii

Uploaded by

nehaalkhasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views46 pages

Unit Iii

Uploaded by

nehaalkhasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

PROBLEM SOLVING USING PYTHON

UNIT-III Loops and Regular Expressions

 Creating Loops with while and for


. The following loops are available in Python to fulfil the looping needs. Python offers 3
choices for running the loops. The basic functionality of all the techniques is the same,
although the syntax and the amount of time required for checking the condition differ.
. We can run a single statement or set of statements repeatedly using a loop command.
. The following sorts of loops are available in the Python programming language.

Sr.No. Name of Loop Type & Description


the loop

1 While loop Repeats a statement or group of statements while a given condition is


TRUE. It tests the condition before executing the loop body.

2 For loop This type of loop executes a code block multiple times and abbreviates
the code that manages the loop variable.

3 Nested loops We can iterate a loop inside another loop.

The for Loop

Python's for loop is designed to repeatedly execute a code block while iterating through a list,
tuple, dictionary, or other iterable objects of Python. The process of traversing a sequence is known
as iteration.

Syntax of the for Loop

1. for value in sequence:


2. { code block }

In this case, the variable value is used to hold the value of every item present in the sequence
before the iteration begins until this particular iteration is completed.

Loop iterates until the final item of the sequence are reached.

SRINIVASARAO G (PROF)-VITAP 1
PROBLEM SOLVING USING PYTHON

Code

1. # Python program to show how the for loop works


2. # Creating a sequence which is a tuple of numbers
3. numbers = [4, 2, 6, 7, 3, 5, 8, 10, 6, 1, 9, 2]
4. # variable to store the square of the number
5. square =
0 6.
7. # Creating an empty list
8. squares =
[] 9.
10. # Creating a for loop
11. for value in numbers:
12. square = value ** 2
13. squares.append(square)
14. print("The list of squares is", squares)

Output:

The list of squares is [16, 4, 36, 49, 9, 25, 64, 100, 36, 1, 81, 4]

Using else Statement with for Loop

As already said,a for loop executes the code block until the sequence element is reached. The
statement is written right after the for loop is executed after the execution of the for loop is
complete.

Only if the execution is complete does the else statement comes into play. It won't be executed if
we exit the loop or if an error is thrown.

Here is a code to better understand if-else statements.

Code

1. # Python program to show how if-else statements work


2. string = "Python Loop"
3. # Initiating a loop
4. for s in a string:
5. # giving a condition in if block

SRINIVASARAO G (PROF)-VITAP 2
PROBLEM SOLVING USING PYTHON
6. if s == "o":

SRINIVASARAO G (PROF)-VITAP 2
PROBLEM SOLVING USING PYTHON

7. print("If block")
8. # if condition is not satisfied then else block will be executed
9. else:
10. print(s)

Output:

P
y
t
h
If block
n

L
If block
If block
p

Now similarly, using else with for loop.

Syntax:

1. for value in sequence:


2. # executes the statements until sequences are exhausted
3. else:
4. # executes these statements when for loop is completed

Code

1. # Python program to show how to use else statement with for loop
2. # Creating a sequence
3. tuple_ = (3, 4, 6, 8, 9, 2, 3, 8, 9, 7)
4. # Initiating the loop
5. for value in tuple_:
6. if value % 2 != 0:
7. print(value)
8. # giving an else statement
9. else:
10. print("These are the odd numbers present in the tuple")

SRINIVASARAO G (PROF)-VITAP 3
PROBLEM SOLVING USING PYTHON

Output:

3
9
3
9
7
These are the odd numbers present in the tuple

The range() Function

With the help of the range() function, we may produce a series of numbers. range(10) will produce
values between 0 and 9. (10 numbers).

We can give specific start, stop, and step size values in the manner range(start, stop, step size).
If the step size is not specified, it defaults to 1.

Since it doesn't create every value it "contains" after we construct it, the range object can be
characterized as being "slow." It does provide in, len, and __getitem__ actions, but it is not an
iterator.

The example that follows will make this clear.

Code

1. # Python program to show the working of range() function


2. print(range(15))
3. print(list(range(15)))
4. print(list(range(4, 9)))
5. print(list(range(5, 25, 4)))

Output:

range(0, 15)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[4, 5, 6, 7, 8]
[5, 9, 13, 17, 21]

SRINIVASARAO G (PROF)-VITAP 4
PROBLEM SOLVING USING PYTHON

To iterate through a sequence of items, we can apply the range() method in for loops. We can
use indexing to iterate through the given sequence by combining it with aniterable's len() function.
Here's an illustration.

Code

1. # Python program to iterate over a sequence with the help of indexing


2. tuple_ = ("Python", "Loops", "Sequence", "Condition", "Range")
3. # iterating over tuple_ using range() function
4. for iterator in range(len(tuple_)):
5. print(tuple_[iterator].upper())

Output:

PYTHON
LOOPS
SEQUENCE
CONDITION
RANGE

While Loop

While loops are used in Python to iterate until a specified condition is met. However, the statement
in the program that follows the while loop is executed once the condition changes to false.

Syntax of the while loop is:

1. while <condition>:
2. { code block }

All the coding statements that follow a structural command define a code block. These statements
are intended with the same number of spaces. Python groups statements together with
indentation.
Code

1. # Python program to show how to use a while loop


2. counter = 0
3. # Initiating the loop
4. while counter < 10: # giving the condition
5. counter = counter + 3

SRINIVASARAO G (PROF)-VITAP 5
PROBLEM SOLVING USING PYTHON
6. print("Python Loops")

SRINIVASARAO G (PROF)-VITAP 5
PROBLEM SOLVING USING PYTHON

Output:

Python Loops
Python Loops
Python Loops
Python Loops

Using else Statement with while Loops

As discussed earlier in the for loop section, we can use the else statement with the while loop
also. It has the same syntax.

Code

1. #Python program to show how to use else statement with the while loop
2. counter = 0
3. # Iterating through the while loop
4. while (counter < 10):
5. counter = counter + 3
6. print("Python Loops") # Executed until condition is met
7. # Once the condition of while loop gives False this statement will be executed
8. else:
9. print("Code block inside the else statement")

Output:

Python Loops
Python Loops
Python Loops
Python Loops
Code block inside the else statement

SRINIVASARAO G (PROF)-VITAP 6
PROBLEM SOLVING USING PYTHON

Single statement while Block

The loop can be declared in asingle statement, as seen below. This is similar to the if-else block,
where we can write the code block in a single line.

Code

1. # Python program to show how to write a single statement while loop


2. counter = 0
3. while (count < 3): print("Python Loops")

 Nested Loops

. A nested loop is a loop inside the body of the outer loop. The inner or outer loop
can be any type, such as awhile looporfor loop. For example, the outer for
loop can contain a while loop and viceversa.
. The outer loop can contain more than one inner loop. There is no limitation on the
chaining of loops.
. In the nested loop, the number of iterations will be equal to the number of
iterations in the outer loop multiplied by the iterations in the inner loop.
. In each iteration of the outer loop inner loop execute all its iteration. For each
iteration of an outer loop the inner loop re-start and completes its execution before
the outer loop can continue to its next iteration.
. Nested loops are typically used for working with multidimensional data structures,
such as printing two-dimensional arrays, iterating a list that contains a nested list.
. In Python, the for loop is used to iterate over a sequence such as a list, string,
tuple, other iterable objects such as range.

. Syntax of using a nested for loop in Python

# outer for loop


for element in sequence
# inner for loop
for element in sequence:
body of inner for loop
body of outer for loop

SRINIVASARAO G (PROF)-VITAP 7
PROBLEM SOLVING USING PYTHON

In this example, we are using a for loop inside a for loop. In this example, we are
printing a multiplication table of the first ten numbers.
. The outer for loop uses therange()function to iterate over the first ten numbers
. The inner for loop will execute ten times for each outer number

. In the body of the inner loop, we will print the multiplication of the outer number and
current number
. The inner loop is nothing but a body of an outer loop.

# outer loop
for i in range(1, 11):
# nested loop

# to iterate from 1 to 10
for j in range(1, 11):
# print multiplication

print(i * j, end=' ')


print()

SRINIVASARAO G (PROF)-VITAP 8
PROBLEM SOLVING USING PYTHON

Output:

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

Nested Loop to Print Pattern


Another most common use of nested loop is to print various star and number
patterns. Let’s see how to use a nested loop to print the following pattern in
Python.

Pattern:
*

* *

* * *

* * * *

* * * * *

SRINIVASARAO G (PROF)-VITAP 9
PROBLEM SOLVING USING PYTHON

Program:

rows = 5
# outer loop
for i in range(1, rows + 1):

# inner loop
for j in range(1, i + 1):

print("*", end=" ")


print('')

While loop inside a for loop


It is very common and helpful to use one type of loop inside another. we can put a while
loopinside the for loop.
Assume we wanted to repeat each name from a list five times.
. Here we will iterate thelistusing an outer for loop

. In each iteration of outer for loop, the inner for loop execute five times to print the
current name five times

names = ['Kelly', 'Jessa', 'Emma']

# outer loop
for name in names:
# inner while loop

count = 0
while count < 5:
print(name, end=' ')

# increment counter
count = count + 1
print()

SRINIVASARAO G (PROF)-VITAP 10
PROBLEM SOLVING USING PYTHON

Output:

Kelly Kelly Kelly Kelly Kelly


Jessa Jessa Jessa Jessa Jessa
Emma Emma Emma Emma Emma

 Loop Control Statements

. Loop control statements change execution from its normal sequence.


. When execution leaves a scope, all automatic objects that were created in that
scope are destroyed.
. Python supports the following control statements. Click the following links to check their
detail.

Let us go through the loop control statements briefly

Sr.No. Control Statement & Description

1 break statement
Terminates the loop statement and transfers execution to the statement immediately
following the loop.

2 continue statement
Causes the loop to skip the remainder of its body and immediately retest its condition
prior to reiterating.

3 pass statement
The pass statement in Python is used when a statement is required syntactically but you
do not want any command or code to execute.

SRINIVASARAO G (PROF)-VITAP 11
PROBLEM SOLVING USING PYTHON

Break

n-=5
while n>0:

n=n-1
if n==2:
break
print(n)
print(‘loop is finished’)

Output:

4
3
Loop is finished

Continue

n-=5
while n>0:
n=n-1
if n==2:
continue
print(n)
print(‘loop is finished’)

Output:

4
3
1
Loop is finished

SRINIVASARAO G (PROF)-VITAP 12
PROBLEM SOLVING USING PYTHON

Pass

. The null statement is another name for the pass statement. A Comment is not ignored
by the Python interpreter, whereas a pass statement is not. Hence, they two are
different Python keywords.
. We can use the pass statement as a placeholder when unsure what code to provide.
So, we only have to place the pass on that line.
. Pass may be used when we don't wish any code to be executed. We can simply
insert a pass in places where empty code is prohibited, such as loops, functions, class
definitions, or if- else statements.

Syntax of the Pass Keyword


Pass
Code
1. sequence = {"Python", "Pass", "Statement", "Placeholder"}
2. for value in sequence:

3. if value == "Pass":
4. pass # leaving an empty if block using the pass keyword

5. else:
6. print("Not reached pass keyword: ", value)

Output:
Not reached keyword: Python
pa
Not reached keyword: Placeholder
ss
Not reached keyword: Statement
pa

SRINIVASARAO G (PROF)-VITAP 13
PROBLEM SOLVING USING PYTHON

Python Regex
. A regular expression is a set of characters with highly specialized syntax that we can
use to find or match other characters or groups of characters.
. In short, regular expressions, or Regex, are widely used in the UNIX world.
. There-module in Python gives full support for regular expressions of Pearl style.
. The re module raises the re.error exception whenever an error occurs while
implementing or using a regular expression.

re.match()

. Python's re.match() function finds and delivers the very first appearance of a
regular expression pattern.
. In Python, the RegEx Match function solely searches for a matching string at the
beginning of the provided text to be searched.
. The matching object is produced if one match is found in the first line.
. If a match is found in a subsequent line, the Python RegEx Match function gives
output as null.
. Examine the implementation for there.match() method in Python.
The expressions ".w*" and ".w*?" will match words that have the letter "w," and
anything that does not has the letter "w" will be ignored.
. The for loop is used in this Python re.match() illustration to inspect for matches for
every element in the list of words.

Matching Characters
. The majority of symbols and characters will easily match.

. (A case-insensitive feature can be enabled, allowing this RE to match Python or


PYTHON.)

. The regular expression check, for instance, will match exactly the string check.

. There are some exceptions to this general rule; certain symbols are special
metacharacters that don't match.

. Rather, they indicate that they must compare something unusual, or they have an effect
on other parts of the RE by recurring or modifying their meaning.

. Here's the list of the metacharacters;

.^$*+?{}[]\|()

SRINIVASARAO G (PROF)-VITAP 14
PROBLEM SOLVING USING PYTHON

Repeating Things

. The ability to match different sets of symbols will be the first feature regular
expressions can achieve that's not previously achievable with string techniques.
. On the other hand, Regexes isn't much of an improvement if that had been their
only extra capacity. We can also define that some sections of the RE must be reiterated a
specified number of times.
. The first metacharacter we'll examine for recurring occurrences is *. Instead of
matching the actual character '*,' * signals that the preceding letter can be matched 0 or
even more times,
rather than exactly one.

Ba*t, for example, matches 'bt' (zero 'a' characters), 'bat' (one 'a' character), 'baaat'
(three 'a' characters), etc.

. Greedy repetitions, such as *, cause the matching algorithm to attempt to replicate the
RE as many times as feasible. If later elements of the sequence fail to match, the
matching algorithm will retry with lesser repetitions.

Python RegEx

A Regular Expression (RegEx) is a sequence of characters that defines a search pattern. For example,

^a...s$

The above code defines a RegEx pattern. The pattern is: any five letter string starting with a
and ending with s .

A pattern defined using RegEx can be used to match against a string.

Expression String Matched?

ab No match
s

^a...s alia Match


$ s

SRINIVASARAO G (PROF)-VITAP 15
PROBLEM SOLVING USING PYTHON

abys Match
s

SRINIVASARAO G (PROF)-VITAP 15
PROBLEM SOLVING USING PYTHON

Expression String Matched?

Alia No match
s

An No match
abacus

Python has a module named re to work with RegEx. Here's an example:

import re

pattern = '^a...s$'
test_string = 'abyss'
result = re.match(pattern, test_string)

if result:
print("Search successful.")
else:
print("Search unsuccessful.")
. Here, we used
re.match( function to search patter within the
test_string . The method
returns a match object if the search is successful. If not, it returns .
Non
. There are other several functions defined in the module to work with RegEx.

MetaCharacters

Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here's a list of

metacharacters:

[].^$*+?{}()\|

SRINIVASARAO G (PROF)-VITAP 16
PROBLEM SOLVING USING PYTHON

[]
- Square
Square brackets specifies a set of characters you wish to match.

Expression Stri Matche

1 match

2 matches
[abc]
Hey
Jude No
match

abcde
ca

[a a or
. Here, will match if the string you are trying to match contains any of the
-
. You can also specify a range of characters using inside square
[a-e]
brackets. is the same as [abcde] .

[1-is the same as [1234] .


[0-39] is the same as [01239] .
. You can complement (invert) the character set by using caret ^ symbol at the start of a
square-
bracket.
[^abc] means any character except a or bor c .
[^0-9] means any non -digit character.

. - Period
A period matches any single character (except newline '\ ).
Expression String Matched?

a No match
.

SRINIVASARAO G (PROF)-VITAP 17
PROBLEM SOLVING USING PYTHON

Expression String Matched?

ac 1 match

ac 1 match
d

acd 2 matches (contains 4 characters)


e

^ - Caret
The caret symbol ^ is used to check if a string starts with a certain character.

Expression String Matched?

a 1 match

ab 1 match
c

No
ba
c

1
ab
c
No match (starts with a but not followed
^a
b

SRINIVASARAO G (PROF)-VITAP 18
PROBLEM SOLVING USING PYTHON
ac
b

SRINIVASARAO G (PROF)-VITAP 18
PROBLEM SOLVING USING PYTHON

$ - Dollar
The dollar symbol $ is used to check if a string ends with a certain character.

Expression String Matched?

1 match

a formul 1
a

No
ca
b

* - Star
The star symbol * matches zero or more occurrences of the pattern left to it.
Expression String Matched?

1
m
n

ma
n
ma* 1

maaa
n
No match (a is not followed

mai
n 1

woma

SRINIVASARAO G (PROF)-VITAP 19
PROBLEM SOLVING USING PYTHON
n

SRINIVASARAO G (PROF)-VITAP 19
PROBLEM SOLVING USING PYTHON

+
-
The plus symbol matches one or more occurrences of the pattern left to it.

Expressi String Matched?

m
n No match (no

ma 1
n matc
h
ma+
maaa
n

No match (a is not followed


mai
n
1

woma
n

- Question Mark

The question mark matches zero or one occurrence of the pattern left to it.
Expression String Matched?

m 1 match

ma 1 match

ma?
n

maaa
No match (more than one a character)

mai No match (a is not followed by n)

SRINIVASARAO G (PROF)-VITAP 20
PROBLEM SOLVING USING PYTHON

Expression String Matched?

woma 1
n

- Braces
Consider this code: {n, . This means atleast , and at most repetitions of the pattern left to it.
Expression String Matched?

No match
abcda
t

1 match (atdaa )
abc
daat
2 matches (at aab anddaaa
a{2,3 )
}
aabc aab daaaa
daaat

aabc
daaaat

Let's try one more example. This RegEx [0 -9]{2, 4} matches atleast 2 digits but not more than 4
digits
Expression String Matched?

ab123csde 1 match (match at


ab123csde)

[0-9] 12 and 345673


{2,4} 3 matches (12 , 3456, 73)

1 and 2
No match

SRINIVASARAO G (PROF)-VITAP 21
PROBLEM SOLVING USING PYTHON

| - Alternation
Vertical bar | is used for alternation (or operator) .

Expression String Matched?

cd
e No

a| ad 1 match (match at
e

3 matches (at
acdbe
acdbe
a

Here, a|b match any string that contains either a orb

- Group
Parentheses () is used to group sub-patterns. For example, (a|b| match any string that matches
either or b or c followed by

Expression String Matched?

abxz No match

(a|b| abx abx

1 match (match at )

axz 2 matches )
axzbc

SRINIVASARAO G (PROF)-VITAP 22
PROBLEM SOLVING USING PYTHON
cabxz

SRINIVASARAO G (PROF)-VITAP 22
PROBLEM SOLVING USING PYTHON

\ - Backslash
Backlash \ is used to escape various characters including all metacharacters. For example,
\$a match if a string contains $ followed by a . Here, $ is not interpreted by a RegEx engine in

a special
way.
If you are unsure if a character has special meaning or not, you can put \ in front of it. This makes

sure

the character is not treated in a special way.

Special Sequences
Special sequences make commonly used patterns easier to write. Here's a list of special
sequences:

\A - Matches if the specified characters are at the start of a string.


Mat
Expression String Matched?

the No
sun

\Athe

In the
sun

Mat
\b - Matches if the specified characters are at the beginning or end of a word.

Expression String Matched?


\bfoo Mat

footbal
l
No

a
football

SRINIVASARAO G (PROF)-VITAP 23
PROBLEM SOLVING USING PYTHON

afootball

SRINIVASARAO G (PROF)-VITAP 23
PROBLEM SOLVING USING PYTHON

Expression String Matched?

the Mat
foo

foo\b Mat
the
afootest

No
the
afootest

\B - Opposite of \ Matches if the specified characters are not at the beginning or end of a word.
b .
Expression String No
Matched?

\ footbal
l No

a Mat
football

No
afootball

foo\B No
the
foo

Mat

the
afootest

SRINIVASARAO G (PROF)-VITAP 24
PROBLEM SOLVING USING PYTHON

the
afootest

SRINIVASARAO G (PROF)-VITAP 24
PROBLEM SOLVING USING PYTHON

\d - Matches any decimal digit. Equivalent to [0 -9]

Expression String Matched?

12abc 3 matches (at 12abc )


3
\d

Pytho
n No

- Matches any non-decimal digit. Equivalent to


[^0-9]

Expressi Stri Matched?

1ab34"5 3 matches 1ab34"5 )


0

134 No match

- Matches where a string contains any whitespace character. Equivalent to [ \t\n\r\f\v] .


Expression String Matched?

1
Python
RegEx
\s No
PythonRegE
x

SRINIVASARAO G (PROF)-VITAP 25
PROBLEM SOLVING USING PYTHON

\S - Matches where a string contains any non-whitespace character. Equivalent to [^ \t\n\r\f\v] .

Expression String Matched?

ab
2 matches (at ab)

\S

No match

- Matches any alphanumeric character (digits and alphabets). Equivalent to


[a-zA-Z0-9_] . By
way, underscore _ is also considered an alphanumeric character.

Expressi Stri Matched?

12&": ; 3 matches (at 12&": ; )

%"> No match

\W - Matches any non-alphanumeric character. Equivalent [^a-zA-Z0-9_]


to

Expression String Matched?

1 match (at
1a2%
1a2%
c

\W
No
Pytho
n

SRINIVASARAO G (PROF)-VITAP 26
PROBLEM SOLVING USING PYTHON

\Z - Matches if the specified characters are at the end of a string.

Expression String Matched?

I like 1
Python

Python\ No
I like Python
Programming

No
Python is
fun.

Python RegEx
Python has a module named re to work with regular expressions. To use it, we need to import the
module.

import re

The module defines several functions and constants to work with RegEx.

re.findall()
The re.findall( method returns a list of strings containing all matches.
Example 1: re.findall()

SRINIVASARAO G (PROF)-VITAP 27
PROBLEM SOLVING USING PYTHON

# Program to extract numbers from a string

import re

string = 'hello 12 hi 89. Howdy 34'


pattern = '\d+'

SRINIVASARAO G (PROF)-VITAP 27
PROBLEM SOLVING USING PYTHON

result = re.findall(pattern, string)


print(result)

# Output: ['12', '89', '34']

If the pattern is not found, re.findall() returns an empty list.

re.split()
The re.spli method splits the string where there is a match and returns a list of strings where the
splits

have occurred.
Example 2: re.split()

import re

string = 'Twelve:12 Eighty nine:89.'


pattern = '\D'

result = re.split(pattern, string)


print(result)

# Output: ['Twelve:', ' Eighty nine:', '.']


If the pattern is not found,re.split( returns a list containing the original string.

You can pass argument to there.split( method. It's the maximum number of splits
maxspli
t

occur.

SRINIVASARAO G (PROF)-VITAP 28
PROBLEM SOLVING USING PYTHON

import re

string = 'Twelve:12 Eighty nine:89 Nine:9.'


pattern = '\d+'

# maxsplit = 1
# split only at the first occurrence
result = re.split(pattern, string, 1)
print(result)

# Output: ['Twelve:', ' Eighty nine:89 Nine:9.']

SRINIVASARAO G (PROF)-VITAP 28
PROBLEM SOLVING USING PYTHON

By the way, the default value ofmaxspli is 0; meaning all possible splits.

re.sub()
The syntax of re.sub() is:

re.sub(pattern, replace, string)

The method returns a string where matched occurrences are replaced with the content

of replac variable.
Example 3: re.sub()

# Program to remove all whitespaces


import re

# multiline string
string = 'abc 12\
de 23 \nf45 6'

# matches all whitespace characters


pattern = '\s+'

# empty string
replace = ''

new_string = re.sub(pattern, replace, string)


print(new_string)

# Output: abc12de23f456
If the pattern is not found,re.sub() returns the original string.

You can pass


coun as a fourth parameter to the re.sub( method. If omited, it results to 0. This will

SRINIVASARAO G (PROF)-VITAP 29
PROBLEM SOLVING USING PYTHON

SRINIVASARAO G (PROF)-VITAP 29
PROBLEM SOLVING USING PYTHON

pattern = '\s+'
replace = ''

new_string = re.sub(r'\s+', replace, string, 1)


print(new_string)

# Output:
# abc12de 23
# f45 6

re.subn()

The re.subn() is similar to re.sub() except it returns a tuple of 2 items containing the new string and
the

number of substitutions made.


Example 4: re.subn()

# Program to remove all whitespaces


import re

# multiline string
string = 'abc 12\
de 23 \nf45 6'

# matches all whitespace characters


pattern = '\s+'

# empty string
replace = ''

new_string = re.subn(pattern, replace, string)


print(new_string)

# Output: ('abc12de23f456', 4)

re.search()
The re.search() method takes two arguments: a pattern and a string. The method looks for the first

location where the RegEx pattern produces a match with the string.

SRINIVASARAO G (PROF)-VITAP 30
PROBLEM SOLVING USING PYTHON

If the search is re.search( returns a match object; if not, it Non .


successful, ) returns e

match = re.search(pattern, str)

Example 5: re.search()

import re

string = "Python is fun"

# check if 'Python' is at the beginning


match = re.search('\APython', string)

if match:
print("pattern found inside the string")
else:
print("pattern not found")

# Output: pattern found inside the string

Here, matc contains a match object.

Match object
You can get methods and attributes of a match object using dir()function.

Some of the commonly used methods and attributes of match objects are:

match.group()

The group( method returns the part of the string where there is a match.
Example 6: Match object

SRINIVASARAO G (PROF)-VITAP 31
PROBLEM SOLVING USING PYTHON

import re

string = '39801 356, 2102 1111'

# Three digit number followed by space followed by two digit number


pattern = '(\d{3}) (\d{2})'

# match variable contains a Match object.

SRINIVASARAO G (PROF)-VITAP 31
PROBLEM SOLVING USING PYTHON

match = re.search(pattern, string)

if match:
print(match.group())
else:
print("pattern not found")

# Output: 801 35
Here, matc variable contains a match object.
Our pattern
these (\d{3}) (\d{2})
parenthesized has two Here's
subgroups. subgroups
how:(\d{3}) and (\d{2}) You can get the part of the string of
.
>>> match.group(1, 2)
('801', '35')

>>> match.groups()

match.start(), match.end() and match.span()

The start( function returns the indexof the start of the matched substring. Similarly,end( returns the
end indexof the matched substring.

>>> match.start()
2
>>> match.end()
8

The span( function returns a tuple containing start and end indexof the matched part.
)

>>> match.span()

SRINIVASARAO G (PROF)-VITAP 32
PROBLEM SOLVING USING PYTHON
(2, 8)

SRINIVASARAO G (PROF)-VITAP 32
PROBLEM SOLVING USING PYTHON

match.re and match.string

The attribute of a matched object returns a regular expression object. Similarly,


string attribute
returns the passed string.
>>> match.re
re.compile('(\\d{3}) (\\d{2})')

>>> match.string
'39801 356, 2102 1111'

Using r prefix before RegEx


When r or R prefix is used before a regular expression, it means raw string. For example, '\n' is a
new

line whereas r'\n' means two characters: a backslash \ followed by n .


Backlash \ is used to escape various characters including all metacharacters. However, using r prefix
makes \ treat as a normal character.
Example 7: Raw string using r prefix

import re

string = '\n and \rare escape sequences.'

result = re.findall(r'[\n\r]', string)


print(result)

# Output: ['\n', '\r']

SRINIVASARAO G (PROF)-VITAP 33

You might also like