-
Notifications
You must be signed in to change notification settings - Fork 36
Introduction to Python
##why Python ?
##Strings
-
Immutable - String don’t change
-
Iterable - It can be travelled
>> s1 = ‘Great Python’ s1 points to a piece of memory holding ‘Great Python’. You can change what s1 points to but cannot change the contents of what s1 points to. >> s1 = ‘great’ // This is valid >> s1 = ‘h’ //This is not valid
###String multiplication >>> s1 * 2 ‘greatgreat’
###String concatination >>> s1 + ‘ ’ + s1 ‘great great’
###Reverse String >>> s ‘dlrow olleh’
None of string operation will mutate the actual string but return new string
##Inbuilt String functions To see do >>> help(str)
###endswith & startswith
>>> s.endswith(‘world’) True >>> s = ‘www.zekelabs.com’ >>> s.ends(‘.com’) Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: ‘str’ object has no attribute ‘ends’ >>> s.endswith(‘.com’) True >>> s.endswith(‘.in’) False >>> s.startswith(‘www’) True
###Difference between find & index
-
find - Pattern may or may not be present
>>> s.find(‘.’,4) 12 >>> s.find(‘hhg’,4) -1 >>> s.index(‘hhg’,4) Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> s.find(‘hhg’) -1 >>> s.find(‘.’) 3 >>> s.rfind(‘.’) 12 >>> s.rfind(‘.’)
###format (({>>> s = “Hi, Mr {name}. Congrats you got {percent}% hike”})) (({>>> s})) (({‘Hi, Mr {name}. Congrats you got {percent}% hike’})) (({>>> s.format(name=“Khan”, percent=200)})) (({‘Hi, Mr Khan. Congrats you got 200% hike’})) (({>>> })) (({>>> s.format(name=“Das”, percent=20)})) (({‘Hi, Mr Das. Congrats you got 20% hike’})) (({>>> s.format(percent=20,name=“Das”)})) (({‘Hi, Mr Das. Congrats you got 20% hike’}))