0% found this document useful (0 votes)
2 views37 pages

Python Unit-5

This document covers file I/O handling and exception handling in Python, detailing types of files, file objects, built-in functions, methods, and attributes for reading and writing operations. It also explains errors in Python, including compile-time, runtime, and logical errors, along with exception handling using try-except blocks. Additionally, it discusses the use of regular expressions and best practices for file operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views37 pages

Python Unit-5

This document covers file I/O handling and exception handling in Python, detailing types of files, file objects, built-in functions, methods, and attributes for reading and writing operations. It also explains errors in Python, including compile-time, runtime, and logical errors, along with exception handling using try-except blocks. Additionally, it discusses the use of regular expressions and best practices for file operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Unit-5 File I/O Handling and Exception Handling

· Types of File

· File Objects, File Built-in Function, File Built-in Methods

· File Built-in Attributes

· Read/write operations Reading Text

· Errors in Python : Compile-Time Errors ,Runtime Errors , Logical


Errors

· What is Exception?

· try….except…else, try-finally clause

· Regular expressions
Types of Files

In Python, files can generally be classified into two types:

●​ Text files: These contain human-readable characters (e.g., .txt, .csv).​

○​ These files store data as a sequence of characters, with each


character represented by a set of bytes, often using ASCII or
UTF-8 encoding.​

●​ Binary files: These contain raw data in binary format (e.g., .jpg, .mp3,
.exe).​

○​ Binary files store data in the form of bytes, and this data may not
be human-readable.
File Objects

When a file is opened in Python using the built-in open() function, a file object
is returned. The file object allows us to interact with the file, read from it, write
to it, or perform other operations.

●​ File Object Definition: An object returned by the open() function that


allows you to read, write, and manipulate the contents of a file.​

Creating a File Object:​


​ file = open("example.txt", "r") # Open a file in read mode

●​ Here, file is the file object used to interact with the file example.txt.

File Built-in Functions

Python provides several built-in functions to open, close, and manage files.
Here are some of the most commonly used functions:

●​ open(filename, mode): This function opens a file and returns a file


object. The mode argument specifies how the file will be used. Common
modes:​
​ 'r': Read (default).​
​ 'w': Write (creates a new file if it doesn’t exist).​
​ 'a': Append (adds content to an existing file).​
​ 'b': Binary mode (e.g., 'rb', 'wb').​
​ 'x': Exclusive creation (fails if the file already exists).​

●​ close(): Closes the file object. It’s important to close a file when you're
done working with it to free up system resources.
●​ flush(): Flushes the internal buffer to the file. It ensures that any data still
in memory gets written to the file.
●​ seek(offset, whence): Moves the file pointer to a specific location.
whence can be 0 (absolute file positioning), 1 (relative to current
position), or 2 (relative to file's end).
●​ tell(): Returns the current position of the file pointer.
File Built-in Methods

File objects have several built-in methods that can be used to perform
operations on the file. These methods provide a convenient way to interact with
files.

1.​ read(size=-1): Reads the entire file (or a specified number of bytes if size
is provided).​

2.​ readline(size=-1): Reads one line from the file. If size is specified, it will
read up to that number of bytes.​

3.​ readlines(hint=-1): Reads the file and returns a list of lines. The hint
parameter specifies the number of bytes to read.​

4.​ write(string): Writes the string to the file. If the file is in write or append
mode, it will overwrite or append the text respectively.​

5.​ writelines(lines): Writes a list of strings to the file.​

File Built-in Attributes


File objects also have several built-in attributes that provide useful information
about the file:
●​ name:
○​ Returns the name of the file.
○​ Syntax: file.name
●​ mode:
○​ Returns the mode in which the file was opened (e.g., 'r', 'w', 'a', 'rb',
'wb').
○​ Syntax: file.mode
●​ closed:
○​ Returns True if the file is closed, False if it's open.
○​ Syntax: file.closed
print(file.closed)
●​ softspace (deprecated):
○​ This attribute used to indicate if a space should be added between
items when printing.
○​ It is now deprecated and is no longer widely used.
print(file.softspace)
Read/Write Operations and Reading Text

1. Read/Write Operations in Python

In Python, reading from and writing to files are common operations. These
operations are done using the open() function and various associated methods
for file objects.

2. Opening a File

Before performing any read/write operation, you need to open a file using the
open() function.

file = open("example.txt", "r") # Opens file in read mode


file = open("example.txt", "w") # Opens file in write mode
file = open("example.txt", "a") # Opens file in append mode

●​ Modes for opening files:


○​ 'r': Read mode (default). Open the file for reading. If the file
doesn't exist, it raises an error.
○​ 'w': Write mode. Open the file for writing. If the file exists, it
truncates the file to zero length, otherwise, creates a new file.
○​ 'a': Append mode. Open the file for appending. If the file doesn't
exist, it creates a new file.
○​ 'rb'/'wb': Binary read/write mode for non-text files (e.g., images).
○​ 'r+': Read and write mode. Opens the file for both reading and
writing.
○​ 'w+': Write and read mode. Opens the file for both writing and
reading.

3. Reading Text from a File

Python provides several methods to read from a file. These methods can be used
depending on how you want to handle the data.

read() Method:
●​ The read() method reads the entire content of the file.
●​ Syntax: file.read(size)
○​ If no size is provided, it reads the entire file.
○​ If size is specified, it reads that number of characters (or bytes in
binary mode).

**s=f.read(4) Reads the first four characters from file

readline() Method:

●​ The readline() method reads a single line from the file.


●​ Syntax: file.readline(size)
○​ If size is specified, it will read up to that number of bytes or
characters.
readlines() Method:

●​ The readlines() method reads all lines in the file and returns them as a list
of strings.
●​ Each element in the list represents a line from the file, including the
newline character \n.

4. Writing Text to a File

You can write data to a file using the write() or writelines() method.

NOTE:

while using open():


1. when open mode is w or a, if file does not exist then new file is
created
2. When open mode is r and file does not exist, then it results in error

write() Method:

●​ The write() method writes a string to the file.


●​ It does not automatically add a newline at the end of the text. You need to
add \n manually if you want new lines.
writelines() Method:

●​ The writelines() method writes a sequence of strings (e.g., list or tuple) to


the file.
●​ Unlike write(), it does not add a newline between the lines. You need to
include the newline character (\n) in the list items.

To store every string in new line:


lst.append(input(“Enter data : ”) + “\n”)

5. Using the with Statement for File Operations

It is a good practice to use the with statement when working with files. It
ensures that the file is properly closed after the block of code is executed, even
if an exception occurs.

with open("example.txt", "r") as file:


content = file.read()
print(content)
# No need to explicitly call file.close()

The with statement automatically handles the opening and closing of the file,
reducing the risk of leaving files open accidentally.

Appending data to file and for loop

6. File Cursor and Positioning

The file cursor (pointer) refers to the current position in the file where the next
read or write operation will happen. You can move the cursor to a specific
position using the seek() method and check its current position using the tell()
method.
seek() Method:

●​ The seek(offset, whence) method moves the file pointer to a given


position.
○​ offset specifies the number of bytes to move.
○​ whence is an optional parameter that specifies the reference
position (default is 0).
■​ 0: Start of the file
■​ 1: Current position
■​ 2: End of the file

Enter data : Ram


Enter data : Shyam
Enter data : Jia
Enter data : Rohan
Enter data : Ria

RamShyamJiaRohanRia

fw.seek(3): ShyamJiaRohanRia

fw.seek(1,0): amShyamJiaRohanRia

fw.seek(11,0): RohanRia

fw.seek(0,1) #while using current position we cannot use non-zero
value as offset
fw.seek(3,1) #non-zero value works with binary mode
fw.seek(-5,2): anRIA #non-zero value works with binary mode

tell() Method:

●​ The tell() method returns the current position of the file pointer.

with open("example.txt", "r") as file:


file.read(5)
position = file.tell() # Get current position of the file pointer
print(position)

Working with binary files: copy image to a file

​open modes are :


rb ​ to read from binary file
wb ​ to write into binary file
ab ​ to append into binary file
rb+ ​ to read and write
wb+ to write and read
ab+ ​ to append and read

f1 = open(“C:\pictures\sea.jpg”,”rb”)


Errors in Python : Compile-Time Errors ,Runtime Errors , Logical Errors

1. Compile-Time Errors:

Definition:

●​ These are errors that occur when the Python code is being converted into
bytecode during the compilation phase.
●​ Python is an interpreted language, but before execution, it undergoes a
compilation step to convert the source code to bytecode. If there’s a
syntax issue, the interpreter can’t complete the compilation process.

Common Causes:

●​ Syntax Errors: When Python can't understand the code due to incorrect
syntax.
○​ Examples:
■​ Misspelled keywords: print(), for, if
■​ Missing parentheses or braces
■​ Unclosed quotes

How to Identify: These errors are usually caught before the program is
executed. If there is a syntax issue, Python will throw a SyntaxError at
the time of compilation.

Example:

print("Hello World) # Missing closing quote

Output:

SyntaxError: EOL while scanning string literal

2. Logical Errors:

Definition:

●​ These errors occur when the program runs without crashing, but it
produces incorrect or unexpected results due to a flaw in the logic of the
code.
●​ The code runs without raising any exceptions, but the output is not what
was intended, making it harder to detect.

Common Causes:

●​ Incorrect calculations
●​ Mistaken conditions in loops or if statements
●​ Wrong algorithm implementation
●​ Misuse of variables or functions

How to Identify: Logical errors do not cause the program to crash, but
they lead to incorrect results. Identifying these errors often requires
reviewing the program logic, output, and verifying against expected
behavior.

Example of a Logical Error that doesn't raise an exception:

def find_max(numbers):
largest = 0 # Logical Error: what if all numbers are negative?
for num in numbers:
if num > largest:
largest = num
return largest

numbers = [-10, -5, -3]


print(find_max(numbers)) # Incorrect output: 0 instead of -3

3. Runtime Errors:

Definition:

●​ These errors occur during the execution of the program, after it has been
compiled successfully.
●​ The program runs, but an issue arises due to invalid operations, or
something goes wrong while executing the code.

Common Causes:

●​ Zero Division Errors: Trying to divide by zero.


●​ Index Errors: Accessing a list element outside its range.
●​ Type Errors: Performing an operation on an inappropriate data type.
●​ File Not Found Errors: Trying to access a file that doesn't exist.

How to Identify: These errors occur when the program is running and
cause the program to stop unexpectedly. Python typically raises
exceptions (e.g., ZeroDivisionError, IndexError, TypeError) to handle
such runtime errors.

Examples:

# ZeroDivisionError
x = 5 / 0 # Error: division by zero

Output:

ZeroDivisionError: division by zero

Example:
# IndexError
my_list = [1, 2, 3]
print(my_list[5]) # Error: list index out of range

Output:

IndexError: list index out of range

Handling Runtime Errors:

●​ Runtime errors can often be caught using try-except blocks to prevent


the program from crashing.

Summary of Errors:
Type Description Example

Compile-Tim Occur when there is incorrect SyntaxError,


e Errors syntax before execution IndentationError

Runtime Occur during execution due to ZeroDivisionError,


Errors invalid operations IndexError, TypeError

Logical Occur when the program runs Incorrect calculations,


Errors but produces incorrect results wrong conditions

What is an Exception?

●​ An exception is an event that disrupts the normal flow of a program. It


occurs when the Python interpreter encounters an error that it cannot
handle during the program's execution.
●​ In Python, exceptions are raised when a program encounters an error or
an unexpected condition. When an exception occurs, Python stops the
normal flow of execution and looks for a block of code that can handle
the exception (usually in the form of try...except blocks).

Types of Exceptions:

●​ Built-in Exceptions: Python has several predefined exceptions that can


occur, such as:​

○​ ZeroDivisionError: Raised when division by zero occurs.


○​ IndexError: Raised when an invalid index is accessed in a
sequence.
○​ ValueError: Raised when a function receives an argument of the
correct type but an inappropriate value.
○​ KeyError: Raised when a dictionary key is not found.
○​ FileNotFoundError: Raised when a file is not found.
●​ Custom Exceptions: You can also define your own exceptions by
creating classes that inherit from the Exception class.​

Built-In Exceptions
Example:

try:
num = int(input("Enter a number: "))
result = 10 / num
print(result)
except ZeroDivisionError:
print("Cannot divide by zero!")
except ValueError:
print("Invalid input! Please enter a valid number.")

In the above code:

●​ If the user enters 0, a ZeroDivisionError will occur.


●​ If the user enters non-numeric input, a ValueError will occur.

try...except...else

●​ The try...except...else block is used to handle exceptions in Python.


●​ The try block lets you test a block of code for errors.
●​ The except block lets you handle the error.
●​ The else block, which is optional, allows you to execute code if the try
block does not raise any exceptions.

Syntax:

try:
# Code that may raise an exception
except ExceptionType:
# Code to handle the exception
else:
# Code to execute if no exception occurs

How it works:

●​ If the code inside the try block runs without an error, the else block will
execute.
●​ If an error occurs inside the try block, Python will jump to the appropriate
except block.

Example:

try:
num = int(input("Enter a number: "))
print(10 / num)
except ZeroDivisionError:
print("You can't divide by zero!")
else:
print("Division successful!")

In the above example:

●​ If no error occurs in the try block, the message is printed in the else
block.
●​ If there's an error in the try block (e.g., ValueError or
ZeroDivisionError), the program will print an error message from
the except block.

try...finally

●​ The try...finally block is used to ensure that certain code is always


executed, whether or not an exception occurs.
●​ The finally block is executed no matter what, even if the code in the try
block raises an exception or is interrupted.
●​ This is often used for cleanup actions (e.g., closing a file, releasing a
resource, or restoring the state).

Syntax:

try:
# Code that may raise an exception
finally:
# Code that will always execute, even if an exception occurs
How it works:

●​ The code in the finally block will execute regardless of whether an


exception occurred in the try block or not. It guarantees that the cleanup
code is always executed.

Example:

try:
file = open("sample.txt", "r")
data = file.read()
except FileNotFoundError:
print("The file does not exist.")
finally:
file.close() # Ensures the file is closed even if an error occurs
print("File closed.")

In this example:

●​ If the file doesn't exist, a FileNotFoundError is caught in the except


block.
●​ Regardless of whether the file is successfully read or not, the
finally block ensures that the file is closed.

Comparison of try...except, try...else, and try...finally

Clause Description When to Use

try...except Catches and handles Use when you expect that an


exceptions. exception may occur and want to
handle it.

try...except...else Executes code if no Use when you want to perform a


exception occurs in the specific action if no exceptions
try block. occur.

try...finally Executes cleanup Use when you need to ensure that


code, no matter what. certain code is always executed,
regardless of exceptions.
Custom/User Defined Exceptions

Output:

Enter rollno : 101


Enter name : John
Rollno is : 101
Name is : John

Enter rollno : 0
Enter name : John
Rollno can't be <= 0

Enter rollno : a
Some problem occured
Regular Expressions

What is a Regular Expression (Regex)?

A regular expression is a sequence of characters that defines a search pattern,


mainly for use in pattern matching with strings.

●​ It is used to check whether a string contains the specified search pattern.


●​ Useful for validation, parsing, data cleaning, and text manipulation.

Common use cases include:

●​ Validating input (e.g., checking if an email address is valid)


●​ Searching for patterns in large text datasets
●​ Replacing parts of a string

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.

Expression String Matched?

^a...s$ abs No match

alias Match

abyss Match

Alias No match

An abacus No match
Python's re Module

To work with regular expressions in Python, import the re module:

import re

Common Functions in re Module

Function Description

re.match(pattern, string) Checks for a match at the beginning of the string

re.search(pattern, string) Searches anywhere in the string

re.findall(pattern, string) Returns a list of all matches

re.sub(pattern, repl, string) Replaces matches with repl

re.split(pattern, string) Splits the string by the occurrences of the pattern

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

if result:
print("Search successful.")
else:
print("Search unsuccessful.")​

test_string=’Helloabyss’ -> Search Unsuccessful

test_string=’abyssHello’ -> Search Unsuccessful


Regex Syntax and Patterns

Quantifiers/Metacharacters
[] - Square brackets: Square brackets specifies a set of characters you wish to
match.

Expression String Matched?

[abc] a 1 match

ac 2 matches

Hey Jude No match

abc de ca 5 matches

You can also specify a range of characters using - inside square brackets.

●​ [a-e] is the same as [abcde].


●​ [1-4] 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 b or c.


●​ [^0-9] means any non-digit character.
. Period: A period matches any single character (except newline '\n').

Expression String Matched?

.. a No match

ac 1 match

acd 1 match

acde 2 matches (contains 4 characters)

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

Expression String Matched?

^a a 1 match

abc 1 match

bac No match

^ab abc 1 match

acb No match (starts with a but not followed by b)


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

Expression String Matched?

a$ a 1 match

formula 1 match

cab No match

* - Star: The star symbol * matches zero or more occurrences of the pattern left
to it.

Expression String Matched?

ma*n mn 1 match

man 1 match

maaan 1 match

main No match (a is not followed by n)

woman 1 match
+ - Plus: The plus symbol + matches one or more occurrences of the pattern left
to it.

Expression String Matched?

ma+n mn No match (no a character)

man 1 match

maaan 1 match

main No match (a is not followed by n)

woman 1 match

? - Question Mark: The question mark symbol ? matches zero or one


occurrence of the pattern left to it.

Expression String Matched?

ma?n mn 1 match

man 1 match

maaan No match (more than one a character)

main No match (a is not followed by n)

woman 1 match
{} - Braces: Consider this code: {n,m}. This means at least n, and at most m
repetitions of the pattern left to it.

Expression String Matched?

a{2,3} abc dat No match

abc daat 1 match (at daat)

aabc daaat 2 matches (at aabc and daaat)

aabc daaaat 2 matches (at aabc and daaaat)

Let's try one more example. This RegEx [0-9]{2, 4} matches at least 2 digits
but not more than 4 digits

Expression String Matched?

[0-9]{2,4} ab123csde 1 match (match at ab123csde)

1 and 2 No match

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

Expression String Matched?

a|b cde No match

ade 1 match (match at ade)

acdbea 3 matches (at acdbea)

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


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

Expression String Matched?

(a|b|c)xz ab xz No match

abxz 1 match (match at abxz)

axz cabxz 2 matches (at axzbc cabxz)

\ - 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.

Expression String Matched?

\Athe the sun Match

In the sun No match


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

Expression String Matched?

\bfoo football Match

a football Match

afootball No match

foo\b the foo Match

the afoo test Match

the afootest No match

\B - Opposite of \b. Matches if the specified characters are not at the


beginning or end of a word.

Expression String Matched?

\Bfoo football No match

a football No match

afootball Match

foo\B the foo No match

the afoo test No match

the afootest Match


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

Expression String Matched?

\d 12abc3 3 matches (at 12abc3)

Python No match

\D - Matches any non-decimal digit. Equivalent to [^0-9]

Expression String Matched?

\D 1ab34"50 3 matches (at 1ab34"50)

1345 No match

\s - Matches where a string contains any whitespace character.

Expression String Matched?

\s Python RegEx 1 match

PythonRegEx No match

\S - Matches where a string contains any non-whitespace character.

Expression String Matched?

\S ab 2 matches (at a b)

No match
\w - Matches any alphanumeric character (digits and alphabets).
Equivalent to [a-zA-Z0-9_]. By the way, underscore _ is also considered an
alphanumeric character.

Expression String Matched?

\w 12&": ;c 3 matches (at 12&": ;c)

%"> ! No match

\W - Matches any non-alphanumeric character. Equivalent to


[^a-zA-Z0-9_]

Expression String Matched?

\W 1a2%c 1 match (at 1a2%c)

Python No match

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

Expression String Matched?

Python\Z I like Python 1 match

I like Python Programming No match

Python is fun. No match


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

Example:
# Program to extract numbers from a string
import re

string = 'hello 12 hi 89. Howdy 34'


pattern = '\d+'

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.split method splits the string where there is a match and
returns a list of strings where the splits have occurred.

Example:
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 maxsplit argument to the re.split() method. It's the
maximum number of splits that will occur.

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.']

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

re.sub(): The method returns a string where matched occurrences are replaced
with the content of replace variable.The syntax of re.sub() is:
re.sub(pattern, replace, string)

Example:
# Program to remove all whitespaces
import re

# multiline string
string = 'abc 12\de
23 \n f45 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 count as a fourth parameter to the re.sub() method. If


omited, it results to 0. This will replace all occurrences.
import re

# multiline string
string = 'abc 12\de 23
f45 6'
# matches all whitespace characters
pattern = '\s+'
replace = ''

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


print(new_string)

# Output:
# abc12de 23 f45 6

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.
If the search is successful, re.search() returns a match object; if not, it returns
None.
Syntax: match = re.search(pattern, str)

Example
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

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 t
​ here is a match.

Example
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.


match = re.search(pattern, string)

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

# Output: 801 35

Here, match variable contains a match object.


Our pattern (\d{3}) (\d{2}) has two subgroups (\d{3}) and (\d{2}). You
can get the part of the string of these parenthesized subgroups. Here's
how:
>>> match.group(1)
'801'

>>> match.group(2)
'35'
>>> match.group(1, 2)
('801', '35')

>>> match.groups()
('801', '35')

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


The start() function returns the index of the start of the matched substring.
Similarly, end() returns the end index of the matched substring.
>>> match.start()
2
>>> match.end()
8
The span() function returns a tuple containing start and end index of the
matched part.
>>> match.span()
(2, 8)
Examples
Check if a string starts with a word
import re
text = "Hello world"
pattern = "^Hello"

match = re.match(pattern, text)


print(bool(match)) # Output: True

Find all digits in a string


import re
text = "There are 3 cats and 4 dogs"
pattern = "\d"

digits = re.findall(pattern, text)


print(digits) # Output: ['3', '4']

Check if an email is valid (very basic check)


import re
email = "[email protected]"
pattern = "\w+@\w+\.\w+"

match = re.match(pattern, email)


print(bool(match)) # Output: True

Replace spaces with dashes


import re
text = "hello world python"
new_text = re.sub("\s", "-", text)
print(new_text) # Output: hello-world-python

Match a phone number (simple format)


import re
text = "Call me at 123-456-7890"
pattern = "\d{3}-\d{3}-\d{4}"
match = re.search(pattern, text)
print(match.group()) # Output: 123-456-7890

Match words that start with a capital letter


import re
text = "Alice and Bob are friends"
pattern = "\b[A-Z][a-z]*"

capital_words = re.findall(pattern, text)


print(capital_words) # Output: ['Alice', 'Bob']

Match repeated characters


import re
text = "soooo good"
pattern = "so{2,}"

match = re.search(pattern, text)


print(match.group()) # Output: sooo

You might also like