100% found this document useful (2 votes)
318 views

Unit-Iii Chapter-1: Python Strings Revisited

Uploaded by

James Gates231
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
318 views

Unit-Iii Chapter-1: Python Strings Revisited

Uploaded by

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

UNIT-III CHAPTER-1

Python Strings Revisited

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Strings
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.
2

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


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

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Concatenating, Appending and Multiplying Strings
Examples:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Strings are Immutable
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.
Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


String Formatting Operator
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>)

Example:

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Built-in String Methods and Functions

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Built-in String Methods and Functions

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Built-in String Methods and Functions

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Slice Operation
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.

Examples:

10

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Specifying Stride while Slicing Strings
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.

Examples:

11

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


ord() and chr() Functions
ord() function returns the ASCII code of the character and chr() function returns character represented by a ASCII
number. Examples:

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.
Examples:

12

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Comparing Strings

13

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Iterating String
String is a sequence type (sequence of characters). You can iterate through the string using for loop.

Examples:

14

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


The String Module
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.
Examples:

15

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Working with Constants in String Module
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.

Examples:

16

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Regular Expressions
Regular Expressions: Regular Expression, is a sequence of characters that forms a search pattern.
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)

17

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


EXAMPLE- Match() Function
Examples:

18

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


The search Function
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.

Example:

19

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


The sub() Function
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.
Example:

20

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


The findall() and finditer() Function
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)

Examples:

21

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Flag Options
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.
22

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Metacharacters in Regular Expression

23

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Metacharacters in Regular Expression

24

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Character Classes
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.

Examples:

25

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Groups
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 ?.

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

•group() returns all groups up from 1. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
UNIT-III CHAPTER-2
File Handling

27

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


File

A file is a collection of data stored on a secondary storage device like hard disk.

A file is basically used because real-life applications involve large amounts of data and in such situations the console

oriented I/O operations pose two major problems:

• First, it becomes cumbersome and time consuming to handle huge amount of data through terminals.

• Second, when doing I/O using terminal, the entire data is lost when either the program is terminated or computer is

turned off. Therefore, it becomes necessary to store data on a permanent storage (the disks) and read whenever

necessary, without destroying the data.

28

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


File Path
Files that we use are stored on a storage medium like the hard disk in such a way that they
can be easily retrieved as and when required.
Every file is identified by its path that begins from the root node or the root folder. In
Windows, C:\ (also known as C drive) is the root folder but you can also have a path that
starts from other drives like D:\, E:\, etc. The file path is also known as pathname.
Relative Path and Absolute Path
A file path can be either relative or absolute. While an absolute path always contains the root
and the complete directory list to specify the exact location the file, relative path needs to be
combined with another path in order to access a file. It starts with respect to the current
working directory and therefore lacks the leading slashes. For example, C:\Students\Under
Graduate\BTech_CS.docx but Under Graduate\BTech_CS.docx is a relative path as only a
part of the complete path is specified. 29

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


ASCII Text Files
A text file is a stream of characters that can be sequentially processed by a computer in forward direction. For this reason a text
file is usually opened for only one kind of operation (reading, writing, or appending) at any given time. Because text files can
process characters, they can only read or write data one character at a time. In Python, a text stream is treated as a special kind
of file.
Depending on the requirements of the operating system and on the operation that has to be performed (read/write operation) on
the file, the newline characters may be converted to or from carriage-return/linefeed combinations. Besides this, other character
conversions may also be done to satisfy the storage requirements of the operating system. However, these conversions occur
transparently to process a text file. In a text file, each line contains zero or more characters and ends with one or more
characters
Another important thing is that when a text file is used, there are actually two representations of data- internal or external. For
example, an integer value will be represented as a number that occupies 2 or 4 bytes of memory internally but externally the
integer value will be represented as a string of characters representing its decimal or hexadecimal value. 30

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Binary Files

A binary file is a file which may contain any type of data, encoded in binary form for computer storage and processing
purposes. It includes files such as word processing documents, PDFs, images, spreadsheets, videos, zip files and other
executable programs. Like a text file, a binary file is a collection of bytes. A binary file is also referred to as a character
stream with following two essential differences.
• A binary file does not require any special processing of the data and each byte of data is transferred to or from the
disk unprocessed.
• Python places no constructs on the file, and it may be read from, or written to, in any manner the programmer
wants.
While text files can be processed sequentially, binary files, on the other hand, can be either processed sequentially or
randomly depending on the needs of the application.
31

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


The Open() Function

Before reading from or writing to a file, you must first open it using Python’s built-in open() function. This function
creates a file object, which will be used to invoke methods associated with it. The syntax of open() is:
fileObj = open(file_name [, access_mode])
Here,
file_name is a string value that specifies name of the file that you want to access.
access_mode indicates the mode in which the file has to be opened, i.e., read, write, append, etc.

Example:

32

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


The open() Function – Access Modes

33

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


The File Object Attributes
Once a file is successfully opened, a file object is returned. Using this file object, you can easily access different type of
information related to that file. This information can be obtained by reading values of specific attributes of the file.

Example:

34

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


The close () Method
The close() method is used to close the file object. Once a file object is closed, you cannot further read from or write
into the file associated with the file object. While closing the file object the close() flushes any unwritten
information. Although, Python automatically closes a file when the reference object of a file is reassigned to another
file, but as a good programming habit you should always explicitly use the close() method to close a file. The syntax
of close() is fileObj.close()
The close() method frees up any system resources such as file descriptors, file locks, etc. that are associated with the
file. Moreover, there is an upper limit to the number of files a program can open. If that limit is exceeded then the
program may even crash or work in unexpected manner. Thus, you can waste lots of memory if you keep many files
open unnecessarily and also remember that open files always stand a chance of corruption and data loss.
Once the file is closed using the close() method, any attempt to use the file object will result in an error.
35

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


The write() and writelines() Methods
The write() method is used to write a string to an already opened file. Of course this string may include numbers,
special characters or other symbols. While writing data to a file, you must remember that the write() method does not
add a newline character ('\n') to the end of the string. The syntax of write() method is: fileObj.write(string)
The writelines() method is used to write a list of strings.

Examples:

36

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


append() Method
Once you have stored some data in a file, you can always open that file again to write more data or append data to it.
To append a file, you must open it using 'a' or 'ab' mode depending on whether it is a text file or a binary file. Note
that if you open a file in 'w' or 'wb' mode and then start writing data into it, then its existing contents would be
overwritten. So always open the file in 'a' or 'ab' mode to add more data to existing data stored in the file.
Appending data is especially essential when creating a log of events or combining a large set of data into one file.

Example:

37

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


The read() and readline() Methods
The read() method is used to read a string from an already opened file. As said before, the string can include, alphabets,
numbers, characters or other symbols. The syntax of read() method is fileObj.read([count])
In the above syntax, count is an optional parameter which if passed to the read() method specifies the number of bytes
to be read from the opened file. The read() method starts reading from the beginning of the file and if count is missing
or has a negative value then, it reads the entire contents of the file (i.e., till the end of file).
The readlines() method is used to read all the lines in the file

Example:

38

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Opening Files using “with” Keyword
It is good programming habit to use the with keyword when working with file objects. This has the advantage that the
file is properly closed after it is used even if an error occurs during read or write operation or even when you forget to
explicitly close the file.

Examples
:

39

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Splitting Words
Python allows you to read line(s) from a file and splits the line (treated as a string) based on a character. By default,
this character is space but you can even specify any other character to split words in the string.

Example:

40

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Some Other Useful File Methods

41

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


File Positions
With every file, the file management system associates a pointer often known as file pointer that facilitate the
movement across the file for reading and/ or writing data. The file pointer specifies a location from where the current
read or write operation is initiated. Once the read/write operation is completed, the pointer is automatically updated.
Python has various methods that tells or sets the position of the file pointer. For example, the tell() method tells the
current position within the file at which the next read or write operation will occur. It is specified as number of bytes
from the beginning of the file. When you just open a file for reading, the file pointer is positioned at location 0, which is
the beginning of the file.
The seek(offset[, from]) method is used to set the position of the file pointer or in simpler terms, move the file pointer
to a new location. The offset argument indicates the number of bytes to be moved and the from argument specifies the
reference position from where the bytes are to be moved.
42

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


File Positions - Example

43

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Renaming and Deleting Files

The os module in Python has various methods that can be used to perform file-processing operations like renaming and
deleting files. To use the methods defined in the os module, you should first import it in your program then call any
related functions.
The rename() Method: The rename() method takes two arguments, the current filename and the new filename. Its
syntax is: os.rename(old_file_name, new_file_name)
The remove() Method: This method can be used to delete file(s). The method takes a filename (name of the file to be
deleted) as an argument and deletes that file. Its syntax is: os.remove(file_name)
Examples
:

44

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Directory Methods
The mkdir() Method: The mkdir()method of the OS module is used to create directories in the current directory. The
method takes the name of the directory (the one to be created) as an argument. The syntax of mkdir() is,
os.mkdir("new_dir_name")
The getcwd() Method: The getcwd() method is used to display the current working directory (cwd).
os.getcwd()
The chdir() Method: The chdir() method is used to change the current directory. The method takes the name of the
directory which you want to make the current directory as an argument. Its syntax is
os.chdir("dir_name")
The rmdir() Method: The rmdir() method is used to remove or delete a directory. For this, it accepts name of the
directory to be deleted as an argument. However, before removing a directory, it should be absolutely empty and all the
contents in it should be removed. The syntax of remove() method is os.rmdir(("dir_name") 45

The makedirs() method: The method mkdirs() is used to create more than one folder. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Directory Methods - Examples

46

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Methods from the os Module

The os.path.abspath() method uses the string value passed to it to form an absolute path. Thus, it is another way to
convert a relative path to an absolute path
The os.path.isabs(path) method accepts a file path as an argument and returns True if the path is an absolute path and
False otherwise.
The os.path.relpath(path, start) method accepts a file path and a start string as an argument and returns a relative
path that begins from the start. If start is not given, the current directory is taken as start.
The os.path.dirname(path) Method returns a string that includes everything specified in the path (passed as argument
to the method) that comes before the last slash.
The os.path.basename(path) Method returns a string that includes everything specified in the path (passed as argument
to the method) that comes after the last slash.
47

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Methods from the os Module

The os.path.split(path) Method: This method accepts a file path and returns its directory name as well as the . So it is
equivalent to using two separate methods os.path.dirname() and os.path.basename()
The os.path.getsize(path) Method: This method returns the size of the file specified in the path argument.
The os.listdir(path) Method: The method returns a list of filenames in the specified path.
The os.path.exists(path) Method: The method as the name suggests accepts a path as an argument and returns True if
the file or folder specified in the path exists and False otherwise.
The os.path.isfile(path) Method: The method as the name suggests accepts a path as an argument and returns True if
the path specifies a file and False otherwise.
The os.path.isdir(path) Method: The method as the name suggests accepts a path as an argument and returns True if
the path specifies a folder and False otherwise.
48

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.


Methods from the os Module — Examples

49

© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.

You might also like