Contents
Python Slicing Strings
In Python, a string is an ordered collection of Unicode characters. every character inside the string has a completely unique index inside the series. The index begins with zero. First individual within the string has its positional index zero. The index keeps incrementing closer to the stop of string.
If a string variable is asserted as var=”HELLO PYTHON”, index of each man or woman within the string is as follows −
Python String Indexing
Python lets in you to get entry to any character man or woman from the string by means of its index. In this situation, 0 is the decrease certain and eleven is the top certain of the string. So, var[0] returns H, var[6] returns P. If the index in square brackets exceeds the upper certain, Python raises Index Error.
Example
my_string = “Python”
# Accessing characters using positive indexing
print(“First character:”, my_string[0])
print(“Third character:”, my_string[2])
# Accessing characters using negative indexing
print(“Last character:”, my_string[-1])
print(“Second last character:”, my_string[-2])
Output
First character: P
Third character: t
Last character: n
Second last character: o
Python String Negative & Positive Indexing
Python is a powerful and versatile programming language that has many features and capabilities. One of these features is negative indexing, which allows you to access elements of a sequence (such as a list, a string, or a tuple) from the end, using negative numbers as indexes
Example
my_string = “Python Programming”
# Positive indexing
print(“First character:”, my_string[0])
print(“Fifth character:”, my_string[4])
# Negative indexing
print(“Last character:”, my_string[-1])
print(“Second last character:”, my_string[-2])
Output
First character: P
Fifth character: o
Last character: g
Second last character: n
Default Values of Indexes with String Slicing
Both the operands for Python’s Slice operator are optional. The first operand defaults to zero, which means if we do not give the first operand, the slice starts of character at 0th index, i.e. the first character. It slices the leftmost substring up to “y-1” characters.
Example
var = “HELLO PYTHON”
# Print the original string
print(“var:”, var)
# Slice the string from index 0 to index 4 (inclusive)
print(“var[0:5]:”, var[0:5])
# Slice the string from the beginning to index 4 (exclusive)
print(“var[:5]:”, var[:5])
Output
var: HELLO PYTHON
var[0:5]: HELLO
var[:5]: HELLO
Example
Similarly, y operand is also optional. By default, it is “-1”, which means the string will be sliced from the xth position up to the end of string.
var = “HELLO PYTHON”
# Print the original string
print(“var:”, var)
# Slice the string from index 6 to index 11 (inclusive)
print(“var[6:12]:”, var[6:12])
# Slice the string from index 6 to the end
print(“var[6:]:”, var[6:])
Output
var: HELLO PYTHON
var[6:12]: PYTHON
var[6:]: PYTHON
Example
Naturally, if both the operands are not used, the slice will be equal to the original string. That’s because “x” is 0, and “y” is the last index+1 (or -1) by default.
var = “HELLO PYTHON”
# Print the original string
print(“var:”, var)
# Slice the string from index 0 to index 11 (inclusive), which is the entire string
print(“var[0:12]:”, var[0:12])
# Slice the string from the beginning to the end, which is equivalent to the entire string
print(“var[:]:”, var[:])
Output
var: HELLO PYTHON
var[0:12]: HELLO PYTHON
var[:]: HELLO PYTHON
The left operand must be smaller than the operand on right, for getting a substring of the original string. Python doesn’t raise any error, if the left operand is greater, bu returns a null string.
Example
var = “HELLO PYTHON”
# Print the original string
print(“var:”, var)
# Slice the string from the last character to the 7th character (inclusive)
# Since the start index is after the end index, this will result in an empty string
print(“var[-1:7]:”, var[-1:7])
# Slice the string from the 7th character to the 1st character (exclusive)
# Since the start index is after the end index, this will result in an empty string
print(“var[7:0]:”, var[7:0])
Output
var: HELLO PYTHON
var[-1:7]:
var[7:0]:
Return Type of String Slicing
Slicing returns a new string. You can very well perform string operations like concatenation, or slicing on the sliced string.
Example
var = “HELLO PYTHON”
# Print the original string
print(“var:”, var)
# Use string slicing to get the first 6 characters,
# then get the first 2 characters from that slice
print(“var[:6][:2]:”, var[:6][:2])
# Slice the string to get the first 6 characters
var1 = var[:6]
# Print the slice
print(“slice:”, var1)
# Get the first 2 characters from the slice
print(“var1[:2]:”, var1[:2])
Output
var: HELLO PYTHON
var[:6][:2]: HE
slice: HELLO
var1[:2]: HE