Professional Documents
Culture Documents
Lists
• a=[2,3,4] or a=[[2,3],[4,5]]
• 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.
>>> l=a[2]
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
>>> 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’
• Mulitilist
name=[[“san”,”man”],[“tan”,”ban”]]
• 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
>>>