Used to access a particular part of list/tuple/string.
a=[10,20,30,40,50]
a[0:3] # Output: 10 20 30
: is slicing operator
number or index to left of colon is included and right of colon is excluded.
When it is my_list[1:5] elements in index from 1 to 4(included) will be printed.
SYNTAX
my_list[starting_index:ending_index:step]
here step is the increment or decrement value, the default value is 1.
Starting at starting_index increment/decrement by step, to ending value will be the operation of slicing operator.
my_list=["helloworld"]
my_list[0:5] # output - hello
my_list[0:5:2] # output - hlo (incrementing by 2)
here the 3rd parameter(step) denotes the increment/decrement.
my_list[::] # output - helloworld
my_list[::-1] # output - (reverses the entire list) dlrowolleh
String slicing:
my_string="HelloWorld"
Example -4
Single and multiple line string
string1 = "I love BTS songs" #single line value is enclosed within double quotes.
string2 = "'I love
Blueming
Celebrity
Through the night
you and I
eight
by IU too''' #multiple line string is enclosed with in triple quotes.
No comments:
Post a Comment