Introduction to Python 143
Evaluation of Expression
The evaluation of an expression produces a value. In an assignment statement, an expression is always there
on the right side.
Let us look at an example of an expression:
>>> 2+3
5 # Output
>>>program=”Hello Python”
>>>program
‘Hello Python’ # Output
>>>print program
Hello Python # Output
An expression is not always a mathematical expression in Python. A value by itself is an expression. In
above example, the “Hello Python” is an expression. The interpreter reads the expression and displays
the string written in quotes as it is.
Note In the given example, we assigned a value “Hello Python” to the variable program.
Now, when we type only program, we get the output ‘Hello Python.’ This is the term we
typed when we assigned a value to the variable. When we use a print statement with
program it gives the value of the variable, i.e., the value after removing quotes.
3.11 StrInG oPeratIonS
The contiguous set of characters kept within quotation marks in Python is termed string. Either single quotes or
double quotes can be used to represent strings. A string in Python can be a combination of alphabets, numerals
and symbols. Mathematical operations cannot be performed on the string even if we have numeral values in it.
There are many operations which can be performed on a string such as slice operator ([]) and [:]),
concatenation operator (+) and repetition operator (*). Slicing is used to take out a subset of the string,
concatenation is used to combine two or more than two strings and repetition is used when we want to repeat
same string several times.
Let us look at an example of how a string is declared in Python.
>>>test=”Test string”
As mentioned earler, a string supports various types of operations:
3.11.1 Concatenation
The concatenation operation is done with the + operator in Python. Concatenation means joining the strings
by linking the last end of the first string with the first end of the second string and so on. Two separate strings
transform into one single string after concatenation.
Let us look at an example of concatenation:
>>>test=”Test string”
>>> “Hello” + test
‘HelloTest string’ # Output
>>>
144 Problem Solving and Python Programming
3.11.2 Repetition
The repetition operation is performed on the strings in order to repeat the string several times. It is done
with * operator.
Let us look at an example of repetition:
>>> ‘Spam’*3
SpamSpamSpam # Output
>>>
3.11.3 Get Particular Character
To access a single item of the string square brackets[] are used. To access the third element of the string, the
string name is typed followed by the [2]. Remember, the index of the string starts with 0 and not 1.
>>>test=”Test string”
>>>test[3]
‘t’ # Output
>>>
3.11.4 Slicing
In Python, you can extract a substring by using a colon inside the square bracket [:]. The resultant substring
is a part of the long string.
>>>test=”Test string”
>>>test[1:7]# substring between index 1 to 7 (excludes 7)
‘est st’ # Output
>>>test[:3] # substring from index 0 to 3(excludes 3)
‘Tes’ # Output
>>>test[2:] # substring from index 2 to last
‘st string’ # Output
>>>
Python also provides various in-built commands or methods for string operation (Table 3.10). These
methods are used to convert the lower case letter to upper case, to determine the length of string, etc.
Table 3.10 List of in-built commands
Method Description
.lower() Convert all upper case letters into lower case.
.upper() Convert all lower case letters into upper case.
.isalpha() Return true if string contain only alphabetical characters.
.isdigit() Return true if string contain only digits characters.
.isspace() Return true if string contain space.
.find(“string”) Return the first index of search string.
.replace(“ old”,”new”) Replace the string with other string.
.count(“character”) Return the occurrence of particular character in string.
len(“string”) Return the length of string.
Introduction to Python 145
Example
>>> s = “Hello Python” #Defining a string
>>> print s.lower()
hello python #Converts the string in lower case
>>> print s.upper()
HELLO PYTHON #Converts the string in upper case
>>> print s.find(“l”)
2 #returns the index of first ‘l’
>>> print s.replace(“l”,”p”)
Heppo Python #Replace each “l” to “p”
>>> print s.count(“o”)
2 #count the number of “o”
>>> print len(s)
12 #Length of the string
>>> s = “Hello”
>>> print s.isalpha()
True #String contains only alphabets
>>> print s.isdigit()
False #String doesn’t contain digits
Check Your Understanding
1. What is slicing?
Ans. In Python, you can extract a substring by using a colon inside the square bracket [:]. The
resultant substring is a part of the long string.
2. What are statement and expression in Python?
Ans. A statement is the instruction that can be interpreted by the Python interpreter. An expression
is a combination of variables, operators, values and reserve keyword.
3.12 boolean exPreSSIonS
A Boolean expression may have only one of two values: TRUE or FALSE.
The simplest Boolean expressions in Python are True and False. The following example uses the operator
==, which compares two operands and prints true if they are equal otherwise print false:
>>> 5 == 5
True # Output
>>> 5 == 6
False # Output
>>> True
True # Output
>>> False
False # Output