Unit 2
Unit 2
Strings in python are surrounded by either single quotation marks, or double quotation marks with
‘=‘.
You can display a string literal with the print() function or Assign String to a Variable.
Multiline Strings:-
You can assign a multiline string to a variable by using three quotes.
Eg: x=“””Good morning all,
I am from MCA section A,
I study Python”””
print(x)
Looping through a string
Eg: for i in “MCA”
Accessing Characters in String
b = "Hello, World!"
print(b[2:5])
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Negative Indexing
•You can specify a range of indexes by specifying where to start and where to end
the range.
•When specifying a range, the return value will be a new list with the specified
items.
Example : Return the third, fourth, and fifth item:
Range of Negative Indexes
Specify negative indexes if you want to start the search from the end of the list:
Example : This example returns the items from "orange" (-4) to, but NOT including
"mango" (-1):
TUPLES
To determine how many items a tuple has, use the len() method
File - Handling
File handling is an important part of any web application.
Python has several functions for creating, reading, updating, and deleting files.
The key function for working with files in Python is the open() function.
The open() function takes two parameters; filename, and mode.
There are four different methods (modes) for opening a file:
"r" - Read - Default value. Opens a file for reading, error if the file does not
exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
Syntax
To open a file for reading it is enough to specify the name of the
file:
f = open("demofile.txt")
The code above is the same as:
f = open("demofile.txt", "rt")
Because "r" for read, and "t" for text are the default values, you
do not need to specify them.
Note: Make sure the file exists, or else you will get an error.
Assume we have the following file, located in the same folder as
Python:
Python File Open - Open a File on the Server.
To open the file, use the built-in open() function.
The open() function returns a file object, which has a read() method
for reading the content of the file:
Ex – 1:
f = open("demofile.txt", "r")
print(f.read())
Read only Parts of the file
By default the read() method returns the whole text, but you can also
specify how many characters you want to return:
Ex – 3 : Return the 5 first characters of the file
f = open("demofile.txt", "r")
print(f.read(5))
readline() method:You can return one line by using the readline() method.
Read one line of the file
f = open("demofile.txt", "r")
print(f.readline())
Close Files
Itis a good practice to always close the file when you are
done with it.
Ex : Close the file when you are finish with it.
f = open("demofile.txt", "r")
print(f.readline())
f.close()
Create a new file
Errors and Exceptions in Python
Errors are the problems in a program due to which the program will
stop the execution. On the other hand, exceptions are raised when
some internal events occur which changes the normal flow of the
program.
Two types of Error occurs in python.
Syntax errors
Logical errors (Exceptions)