# Understanding slice notation with examples in Python

Python slice notation example: Here, we are going to learn through the examples, how slice notation works with lists, strings, etc in Python programming language?
Submitted by IncludeHelp, on April 25, 2020

Syntaxes to use slice notation

object[begin:end]   # starts with begin, stops at end-1
object[end:]        # start with begin, and rest of the items
object[:stop]       # starts from 0, stops at end-1
object[:]           # returns a copy of whole array
object[begin:end:step]  # starts with begin, stops at end-1 with given step

## Python program for understanding slice notation

x = [10, 20, 30, 40, 50] # list
y = (10, 20, 30, 40, 50) # tuple
z = "https://www.includehelp.com/" # string

print("x =", x)
print("y =", y)
print("z =", z)
print()

print("x[0:5] =", x[0:5])
print("x[0:4] =", x[0:4])
print("x[2:5] =", x[2:5])
print("x[0:] =", x[0:])
print("x[:5] =", x[:5])
print("x[:3] =", x[:3])
print("x[0:5:2] =", x[0:5:2])
print()

print("y[0:5] =", y[0:5])
print("y[0:4] =", y[0:4])
print("y[2:5] =", y[2:5])
print("y[0:] =", y[0:])
print("y[:5] =", y[:5])
print("y[:3] =", y[:3])
print("y[0:5:2] =", y[0:5:2])
print()

print("z[0:5] =", z[0:5])
print("z[0:4] =", z[0:4])
print("z[2:5] =", z[2:5])
print("z[0:] =", z[0:])
print("z[:5] =", z[:5])
print("z[:3] =", z[:3])
print("z[0:5:2] =", z[0:5:2])
print()

Output

x = [10, 20, 30, 40, 50]
y = (10, 20, 30, 40, 50)
z = https://www.includehelp.com/

x[0:5] = [10, 20, 30, 40, 50]
x[0:4] = [10, 20, 30, 40]
x[2:5] = [30, 40, 50]
x[0:] = [10, 20, 30, 40, 50]
x[:5] = [10, 20, 30, 40, 50]
x[:3] = [10, 20, 30]
x[0:5:2] = [10, 30, 50]

y[0:5] = (10, 20, 30, 40, 50)
y[0:4] = (10, 20, 30, 40)
y[2:5] = (30, 40, 50)
y[0:] = (10, 20, 30, 40, 50)
y[:5] = (10, 20, 30, 40, 50)
y[:3] = (10, 20, 30)
y[0:5:2] = (10, 30, 50)

z[0:5] = https
z[0:4] = http
z[2:5] = tps
z[0:] = https://www.includehelp.com/
z[:5] = https
z[:3] = htt
z[0:5:2] = hts