Python - Slicing Lists and Strings
** Slicing Lists and Strings
==========================================
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
# -10,-9,-8,-7,-6,-5,-4,-3,-2,-1
# list[start:end:step]
print(my_list)
print('-'*80)
print(my_list[0])
print(my_list[5])
print('-'*80)
print(my_list[-1])
print(my_list[-10])
print('-'*80)
print(my_list[0:6])
print(my_list[3:8])
print('-'*80)
print(my_list[-7:-2])
print(my_list[1:-2])
print(my_list[1:9])
print('-'*80)
print(my_list[1:])
print(my_list[5:])
print('-'*80)
print(my_list[-1:])
print('-'*80)
print(my_list[2:-1:2])
print(my_list[2:-1:1])
print(my_list[-1:2:-1])
print(my_list[-2:1:-2])
print('-'*80)
print(my_list[::-1])
---------------------------------
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
--------------------------------------------------------------------------------
0
5
--------------------------------------------------------------------------------
9
0
--------------------------------------------------------------------------------
[0, 1, 2, 3, 4, 5]
[3, 4, 5, 6, 7]
--------------------------------------------------------------------------------
[3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7, 8]
--------------------------------------------------------------------------------
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
--------------------------------------------------------------------------------
[9]
--------------------------------------------------------------------------------
[2, 4, 6, 8]
[2, 3, 4, 5, 6, 7, 8]
[9, 8, 7, 6, 5, 4, 3]
[8, 6, 4, 2]
--------------------------------------------------------------------------------
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
---------------------------------
==========================================
sample_url = 'https://google.com'
print(sample_url)
print('-'*80)
# Reverse the url
print(sample_url[::-1])
print('-'*80)
# Get the top level domain
print(sample_url[-4:])
print('-'*80)
# Print the url without the https://
print(sample_url[8:])
print('-'*80)
# Print the url without the https:// or the top level domain
print(sample_url[8:-4])
print('-'*80)
---------------------------------
https://google.com
--------------------------------------------------------------------------------
moc.elgoog//:sptth
--------------------------------------------------------------------------------
.com
--------------------------------------------------------------------------------
google.com
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
---------------------------------