In this tutorial, we are going to study how to use the slice operator in python. The slice operator is used to slice the string i.e [n:m] it will return the part of the string starting from the nth character to the mth character including the first but excluding the last. The slice() in python is used for a similar purpose.
What is slicing?
Slicing means getting the substring(subpart) from the string(or an iterable). It is done in python with the help of brackets. It helps to get access to the parts of sequences like strings, tuples, and the list. Slicing syntax comes with three parameters start, stop and step.
start: Starting index where the slicing of the object starts
stop: Ending index where the slicing of the object is stopped
step: This argument determines the increment between each index for slicing
More ways to use the slice operator:
#slicing done from start index to stop index -1 Slice[start:stop] #slicing from start index to the end slice[start:] #slicing done from the beginning to index stop-1 Slice[:stop]
Examples of Slice operator in Python
string1 = 'Welcome to copyassignment.com?' print("String = ",string1) # Slice print("Slicing from start index to stop-1 = ",string1[4:8]) print("Slicing from start index to the end = ",string1[6:]) print("Slicing from the beginning to index stop - 1 = ",string1[:5]) print("Slicing from the start index to stop index, by skipping step = ",string1[5:11:2])
string1 = 'Welcome to copyassignment.com?' print("String = ",string1) # Slice print("String after slicing and using step = ",string1[4:12:2])
We can perform the slicing on the tuples and use the step parameter to do the increment between the indexes.
# Create a Tuple tuplelist = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October") print("Tuple = ",tuplelist) # Slice the Tuple print("Tuple after slicing = ",tuplelist[2:8:2])
We can perform the slicing on the different parts and also use step parameters to set the increment index.
# Create a List Lists = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'] print("List = ",Lists) # Slice the List print("List after slicing = ",Lists[2:9:3])
Thank you for visiting our website.
- Filter List in Python | 10 methods
- Python | Check if a string is a palindrome or not without Recursion
- Python | Check if a number is an Armstrong Number or not using Recursion
- Python | Check if a number is an Armstrong Number or not without using Recursion
- Python | Shuffle a list using recursion
- Python | Shuffle a list without recursion
- Python | Implementing switch case using functions
- Python | Find LCM using function
- Python | Find HCF using function
- Python | Convert the binary number to decimal without using library function
- Python | Create a basic operations calculator(+, -, /, and x), create a separate function for each operation
- Python | Detecting the number of local variables declared in a function
- Python | Making a chain of function decorators (bold italic underline etc)
- Python | Access function inside a function
- Python | Create a function with a pass statement
- Python | Function to calculate the square root of a number
- Python | A function that calculates the power of a number
- Python | A function that accepts 2 integers and adds them and returns their sum
- Python | Function that takes a list of integers and returns the last integer
- Python | Return multiple values from a function
- Python function that takes a list and returns a new list with unique elements of the first list
- Python | Generate a random number in the range of 1 to 10 using the function
- Python | Check Whether a Given Number is Even or Odd using Recursion
- Python | Print Binary Equivalent of an Integer using Recursion
- Python | Print Binary Equivalent of a Number without Using Recursion
- Python | Reverse a string using recursion
- Python | Find Sum of Digit of a Number Without Recursion
- Python | Reverse a string without recursion
- Python | Reverse a list using recursion
- Python | Print Hello world using function