You are on page 1of 44

String Handling

Lists

• Lists are the most versatile of Python's compound data


types. A list contains items separated by commas and
enclosed within square brackets ([]). To some extent,
lists are similar to arrays in C. One difference between
them is that all the items belonging to a list can be of
different data type.
• The values stored in a list can be accessed using the
slice operator ([ ] and [:]) with indexes starting at 0 in
the beginning of the list and working their way to end -
1. For example −

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]


Lists
• Set of Homogenous as well as heterogeneous
data elements

• Can be one dimensional or Multidimensional

• a=[2,3,4] or a=[[2,3],[4,5]]

• Print a[2] or print(a[0][0])


Array with Indexing
• a=[2,3,4]

• Print a[0:2]
Means print 2 values which start from 0 index

it will print 2 3
String
• String is combination of more than one
character.

• We can say it is an array of characters.

for example : a019 , 019 , hello, etc.


Simple examples

>>> a=“guru nanak dev university”

>>> l=a[2]

>>>print(l) or with indexing print(a[0:4])


Unicode String

• Normal strings in Python are stored internally


as 8-bit ASCII, while Unicode strings are stored
as 16-bit Unicode. This allows for a more
varied set of characters, including special
characters from most languages in the world.
• Example
>>> s = u"\U0001F4A9"
>>> len(s) will print 1
Read a String
• Name=input()

hello world

What happened
name= “ san” “man” “tan” as input (consider
it one string)
String operators
+ >

* <

[] ==

[:]

in

not in
String Manipulation

>>> name=“TCS”
>>> len=0
>>> for char in name
………….. len=len+1
>>>print(len)
String Manipulation
• Substring in a String

>>> name=“TCS is day one company”


>>>print(name[0:3])
String Handling Function
• length

>>> len(name)

• String Comparison
s1=“hello”
s2=“hello1”
> == <
Compare Two Strings
string1 = input("Enter first string: ")
string2 = input("Enter second string: ")
if string1 == string2:
print("\nBoth strings are equal to each other.")
print(string1,"==",string2)
else: print("\nStrings are not equal.")
print(string1,"!=",string2)
String Handling Function
• capitalize()
Capitalizes first letter of string

>>> name=“sandeep”;
>>> print (name.capitalize())
count()
• Syntax
str.count(sub, start= 0,end=len(string))

Example
str=“ guru nanak dev”
Sub=“u”
l=str.count(sub,5,40)
find()
• str.find(str, beg=0, end=len(string))

• Return -1 if not
• Otherwise return index value
Upper and lower
• str.lower()

• str.upper()
String are immutable

>>>name=“hello”
>>>name[0]=‘b’

what will be the out put ???


String as List
• name=[“san”,”man”,”tan”]

• Mulitilist

name=[[“san”,”man”],[“tan”,”ban”]]

known as two dimensional list can be accessible as


print(name[1][1])
String inbuilt functions

• join()

• split()

• replace()

• reversed()
>>> color_list=["Red", "Blue", "Green", "Black"]
>>> print(color_list)
['Red', 'Blue', 'Green', 'Black']
>>> color_list.insert(2, "White") #Insert an item at
third position
>>> print(color_list)
['Red', 'Blue', 'White', 'Green', 'Black']
>>>
>>> color_list=["Red", "Blue", "Green", "Black"]
>>> print(color_list)
['Red', 'Blue', 'Green', 'Black']
>>> color_list.remove("Black")
>>> print(color_list)
['Red', 'Blue', 'Green']
>>> color_list=["Red", "Blue", "Green", "Black"]
>>> print(color_list)
['Red', 'Blue', 'Green', 'Black']
>>> color_list.clear()
>>> print(color_list)
[]
>>>
>>> color_list=["Red", "Blue", "Green", "Black"]
>>> print(color_list)
['Red', 'Blue', 'Green', 'Black']
>>> color_list.pop(2) # Remove second item
and return it
'Green'
>>> print(color_list)
['Red', 'Blue', 'Black']
>>>
>>> color_list=["Red", "Blue", "Green", "Black"]
>>> print(color_list)
['Red', 'Blue', 'Green', 'Black']
>>> color_list.index("Red")
0
>>> color_list.index("Black")
3
>>>

You might also like