0% found this document useful (0 votes)
7 views21 pages

Ch7. Strings: Dr. Tulika Assistant Professor Department of Computer Science Miranda House

The document provides an overview of strings in Python, explaining that strings are sequences of characters treated as objects of the str class. It covers string creation, indexing, immutability, and basic string operations such as slicing, concatenation, and membership testing. Additionally, it highlights the use of built-in functions like min(), max(), and len() for string manipulation.
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)
7 views21 pages

Ch7. Strings: Dr. Tulika Assistant Professor Department of Computer Science Miranda House

The document provides an overview of strings in Python, explaining that strings are sequences of characters treated as objects of the str class. It covers string creation, indexing, immutability, and basic string operations such as slicing, concatenation, and membership testing. Additionally, it highlights the use of built-in functions like min(), max(), and len() for string manipulation.
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/ 21

Dr.

Tulika

CH7. STRINGS Assistant Professor


Department of Computer Science
Miranda House
7.1 INTRODUCTION
Characters are building blocks of Python. A program is composed of a sequence of
characters.
When a sequence of characters is grouped together, a meaningful string is created.
Thus, a string is a sequence of characters treated as a single unit.
In many languages, strings are treated as arrays of characters but in Python a string
is an object of the str class.
7.2 STR CLASS
Strings are objects of the str class. We can create a string using the constructor of str
class as:
S1=str() #Creates an Empty string Object
S2=str(“Hello”) #Creates a String Object for Hello
An alternative way to create a string object is by assigning a string value to a
variable.
Example
S1 = “” # Creates an Empty String
S2= “Hello” # Equivalent to S2=str(“Hello”)
All the characters of a string can be accessed at one time using the index operator.
7.3 BASIC INBUILT PYTHON
FUNCTIONS FOR STRING
min() and max() functions to return the largest and smallest character in a string. We
can also use len() function to return the number of characters in a string.
7.4 THE INDEX[] OPERATOR
As a string is a sequence of characters, the characters in a string can be accessed one
at a time through the index operator.
The characters in a string are zero based, i.e. the first character of the string is stored
at the 0th position and the last character of the string is stored at a position one less
than that of the length of the string.
Note: Consider a string of length ‘n’, i.e. the valid indices for such string are from 0 to
n-1. If you try to access the index greater than n-1, Python will raise a ‘string index
out of range’ error.
7.4.1 ACCESSING CHARACTERS VIA
NEGATIVE INDEX
The negative index accesses characters from the end of a string by counting in
backward direction.
The index of the last character of any non-empty string is always -1.
Note:
S[-n] == S[Length _ of(S)-n]
Example:
S=“IIT-Bombay”
>>> S[-3]
>>>‘b’
7.5 TRAVERSING STRING WITH FOR
AND WHILE LOOP
A programmer can use the for loop to traverse all characters in a string.
7.5.1 TRAVERSING WITH A WHILE
LOOP
A programmer can also use the while loop to traverse all the elements of a string.
7.6 IMMUTABLE STRINGS
Character sequences fall into two categories, i.e. mutable and immutable. Mutable
means changeable and immutable means unchangeable. Hence, strings are immutable
sequences of characters.
Python uses one object for each string which has the same content.
7.7 THE STRING OPERATORS
String contains the slicing operator and the slicing with step size parameter is used to
obtain the subset of a string. It also has basic concatenation ‘+’, ‘ in’ and repetition ‘*’
operators.
7.7.1 The String Slicing Operator [start: end]
The slicing operator returns a subset of a string called slice by specifying two indices,
viz. start and end. The syntax used to return a subset of a string is:
Name_of_Variable_of_a_String[ Start_Index: End_Index]
Example
>>> S=“IIT-BOMBAY”
>>> S[4:10] #Returns a Subset of a String
‘BOMBAY’
The S[4:10] returns a subset of a string starting from start index, i.e. 4 to one index
less than that of end parameter of slicing operation, i.e. 10 - 1 = 9.
>>> S[:-1]
#start with the character stored at index 0 & exclude the last character stored
at index -1.
‘IIT-MADRA’
7.7.2 STRING SLICING WITH STEP
SIZE
7.7.3 THE STRING +, * AND IN
OPERATORS
1. The + Operator: The concatenation operator ‘+’ is used to join two strings.
Example:
>>> S1=“IIT” #The String “IIT” assigned to S1
>>> S2=“Delhi” #The String “Delhi” assigned to S1
>>> S1+S2
‘IIT Delhi’
2. The * Operator: The multiplication (*) operator is used to concatenate the same
string multiple times. It is also called repetition operator.
Example:
>>> S1=“Hello”
>>> S2=3*S1 #Print the String “Hello” three times
>>> S2
‘HelloHelloHello’
3. The in and not in Operator: Both Operators in and not in are used to check whether
a string
is present in another string.
Example:
>>> S1=“Information Technology” #Check if the string “Technology” is present
in S1
>>> “Technology” in S1
True #Check if the string “Technology” is present in S1
>>> “Engineering” in S1
False
>>> S1=”Information Technology” # Check if the string “Hello” is not present in S1
>> “Hello” not in S1
True

You might also like