Untitled Document
Slicing - List & Strings !!
1. Slicing Introduction Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Slicing Introduction
       
       # here is the general syntax for slicing list and strings 
       # [start:end:step]
       # lets execute all the possible examples and understand how slicing works in Python
       
2. Simple example of slicing a list Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Simple example of slicing a list
       
       # Below example will print elements from 0th index to 4th index 
       my_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
       print(my_list[0:5])
       
3. Printing list element from given index till end of list Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Printing list element from given index till end of list
       
       my_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
       print(my_list[1:])
       
4. Printing list of elements from begining to second last element Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Printing list of elements from begining to second last element
       # IMP -1 represents last element of the list 
       
       my_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
       print(my_list[:-1])
       
5. Printing Complete List Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Printing Complete List
       
       my_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
       print(my_list[:])
       
       
6. Printing list of elements with +ve and -ve index Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Printing list of elements with +ve and -ve index
       
       my_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
       print(my_list[2:-2])            
       print(my_list[-1:2])
 
       
7. Printing list elements with steps Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Printing list elements with steps
       # Here we will give step to print elemnet i.e in below example the list will print alternate character; you can put step as per your
       # choice based on size of index 
       
       # EX - 1
       my_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
       print(my_list[2:-1:2])  
       
       # EX - 2  Printing list elements with steps using -ve steps ( i.e in reverse order)  
       
       my_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
       print(my_list[-1:2:-1])
       
       # EX -3 Printing complete List in reverse order 
       
       my_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
       print(my_list[::-1])
       
8. Slicing String Examples Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Slicing String Examples 
       # Here we will see different examples on how we can perform slicing on strings 
       
       # EX - 1 Reversing the String 
       
       sample_url = "http://mangadaku.com/"
       print(sample_url[::-1])
       
       # EX - 2 Get the top level domain (i.e .com)
       
       sample_url = "http://mangadaku.com/"
       print(sample_url[-4:])
       
       # EX - 3 Print the url without http://
       
       sample_url = "http://mangadaku.com/"
       print(sample_url[7:])
       
       # EX - 4 Print the url without http:// and the top level domain (.com)
       
       sample_url = "http://mangadaku.com/"
       print(sample_url[7:-5])
       
       
Untitled Document