Python-Unit2
Python-Unit2
Lists
Strings
• A string is a sequence of characters. You can access the characters
one at a time with the bracket operator. [ ]
• Strings are used in different applications such as:
• Natural language Processing.
• Regular expression.
• Data mining.
• Dictionaries.
• Chatbot.
• Machine translation.
sub=“Python”
Using Strings
Fruit= “banana”
- Fruit is now a string object.
- Every character in a string has a index value which starts from 0.
- When accessing the string characters, we make use of indices inside
the square brackets. For ex: Fruit[0] is b, Fruit[1] is a …….
- Index is always an integer number. It can be a variable or an
expression.
Getting the length of a string
• The len() function.
• Ex:
length= len(Fruit)
- The output is 6.
- But Fruit[6] is an error, as the index for “banana” starts at 0 and ends
at 5.
- To get the last character, use Fruit[length-1]
- Alternatively, negative indices can also be used.
- Fruit[-1],Fruit[-2]….
String Traversal using loops
• Using a while loop.
Using a for loop
String slicing
• A segment of a string is called a slice. Selecting a slice can be done
using the character ‘:’
Strings are immutable
• String objects once created cannot be altered or changed at any point
in the program.
• S= “Jello”
• S[0]=‘H’ # Error
• Solution:
S1=‘H’+S[1:]
Looping and Counting
• Counting number of characters in a string
word = 'banana'
count = 0
for letter in word:
if letter == 'a':
count = count + 1
print(“count of a’s:”, count)
The in operator
• in is a Boolean operator that takes 2 strings, returns True if the first
string appears as a substring of second string.
• Ex:
‘s’ in ‘words’ #True
‘ors’ in ‘words’ #False
• not in will return True if the first string does not appear in the second.
String Comparison
• The comparison operator ‘==‘ works on strings also.
• Other comparison operators such as <,> can also be used.
They compare the ASCII values of the first character of the
strings.
Strings Methods
• Every object in python is associated with data and methods.
Methods are built into the object and are available to any
instance of that object.
• The function dir() shows all the available methods for that
object.
s=“python”
dir(s)
a=[1,2,3]
b=[1,2,3]
Here, a and b are 2 different objects.
Object Aliasing
• The same object may be called by different names.
• Write a program to check if the given key is present in the
list, if yes, print its position.
• Write a program to remove duplicates from a list.
• Consider the given scenario: In a bank, a cashier is serving a
queue of customers with token number starting from 101.
There are 20 customers standing in the queue. The cashier is
a slow server and takes 5 mins to serve each customer. To
avoid waiting time, the manager appoints another cashier
who serves only even token numbers. Now the first cashier
serves odd token numbers and second, the even ones. Write
a program to create 2 queues of customers and cashiers
serve them in the increasing order of token numbers.