String Slicing in Python

21 Jun, 2022
Python slicing is about obtaining a sub-string from the given string by slicing it respectively from start to end. 
For understanding slicing we will use different methods, here we will cover 2 methods of string slicing, the one is using the in-build slice() method and another using the [:] array slice. String slicing in Python is about obtaining a sub-string from the given string by slicing it respectively from start to end. 
Using a slice() method
Using array slicing  [ : : ] method
Index tracker for positive and negative index: String indexing and slicing in python. Here, the Negative comes into consideration when tracking the string in reverse. 
The slice() constructor creates a slice object representing the set of indices specified by range(start, stop, step).
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Syntax:
slice(stop)
slice(start, stop, step)
Parameters: start: Starting index where the slicing of object starts. stop: Ending index where the slicing of object stops. step: It is an optional argument that determines the increment between each index for slicing. Return Type: Returns a sliced object containing elements in the given range only. 
Example:
Python3
# Python program to demonstrate# string slicing # String slicingString = 'ASTRING' # Using slice constructors1 = slice(3)s2 = slice(1, 5, 2)s3 = slice(-1, -12, -2) print(& quot       String slicing & quot       )print(String[s1])print(String[s2])print(String[s3])
String slicing
AST
SR
GITA
In Python, indexing syntax can be used as a substitute for the slice object. This is an easy and convenient way to slice a string using list slicing and Array slicing both syntax-wise and execution-wise. A start, end, and step have the same mechanism as the slice() constructor. 
Below we will see string slicing in Python with example.
Syntax
arr[start:stop]         # items start through stop-1
arr[start:]             # items start through the rest of the array
arr[:stop]              # items from the beginning through stop-1
arr[:]                  # a copy of the whole array
arr[start:stop:step]    # start through not past stop, by step
Example 1:
In this example, we will see slicing in python list the index start from 0 indexes and ending with a 2 index(stops at 3-1=2 ).
Python3
# Python program to demonstrate# string slicing # String slicingString = 'GEEKSFORGEEKS' # Using indexing sequenceprint(String[:3])
Output:
GEE
Example 2:
In this example, we will see the example of starting from 1 index and ending with a 5 index(stops at 3-1=2 ), and the skipping step is 2. It is a good example of Python slicing string by character.
Python3
# Python program to demonstrate# string slicing # String slicingString = 'GEEKSFORGEEKS' # Using indexing sequenceprint(String[1:5:2])
Output:
EK
Example 3:
In this example, we will see the example of starting from -1 indexes and ending with a -12 index(stops at 3-1=2 )and the skipping step is -2.
Python3
# Python program to demonstrate# string slicing # String slicingString = 'GEEKSFORGEEKS' # Using indexing sequenceprint(String[-1:-12:-2])
Output:
SEGOSE
Example 4:
In this example, the whole string is printed in reverse order.
Python3
# Python program to demonstrate# string slicing # String slicingString = 'GEEKSFORGEEKS' # Prints string in reverseprint(String[::-1])
Output:
SKEEGROFSKEEG
Note: To know more about strings click here.
surajkumarguptaintern
python-string
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Convert integer to string in Python
Python | os.path.join() method
Create a Pandas DataFrame from Lists
Introduction To PYTHON