Slicing is a broader concept applicable to sequences like lists, tuples, and strings. It involves extracting a portion, or a "slice," of a sequence based on indices. It is not an in-built function, it is a concept.
art_board = "ArtBoard1"
list_of_shapes = ["Square", "Triangle", "Circle"]
tuple_of_colors = ('59D5E0', 'F5DD61', 'FAA300', 'F4538A')
# Slice works on both string and Iterables
# Get first 3 characters
print(art_board[:3])
# Reverse the list
print(list_of_shapes[::-1])
print(tuple_of_colors[1::2])
Output:
Art
['Circle', 'Triangle', 'Square']
('F5DD61', 'F4538A')
Split is used to divide a string into a list of substrings basedon a specified delimiter.
By default, the delimiter is a space.
# Split only works on String and create a list as outcome
my_name = "Priyanka Chakraborti"
my_first_name = my_name.split()[0]
my_last_name = my_name.split()[1]
print(my_first_name, my_last_name, sep=',')
print(my_name.split())
# Change list to Tuple
print(tuple(my_name.split()))
Output:
Priyanka,Chakraborti
['Priyanka', 'Chakraborti']
('Priyanka', 'Chakraborti')
No comments:
Post a Comment