Learnitweb

Slicing String in Python

Slicing is used to obtain a sub-string from a given string by slicing it from a given start and end. Here start and end mean the index within a given string. There is another optional parameter step which indicates the increment between each index for slicing.

There are two ways to slice a Python string:

  • slice() constructor
  • Extending indexing syntax

slice()

slice() is a constructor that creates a Python slice object to represent the set of indices that range (start, end, stop) specifies. slice() can be used to slice any sequence. Since string is a sequence, we’ll use slice() to slice a string.

Note: slice() creates a sliced object.

slice() syntax

  • slice(stop)
  • slice(start, stop, step)

In the slice() syntax:

  • start is index to start slicing. This is optional.
  • end is index to end slicing. Slicing stops at index end-1.
  • step is optional and means the increment between each index for slicing.

Example – slice() with only ‘stop’ argument

str = 'abcdefghijkl'

#slice with only 'stop' argument 
slicedObjWithOnlyStop = slice(10)

# print sliced object
print(slicedObjWithOnlyStop)

#print substring using slicedOject
print(str[slicedObjWithOnlyStop])

Output

slice(None, 10, None)
abcdefghij

Example 2 – slice() with ‘start’ and ‘stop’ arguments

str = 'abcdefghijkl'

#slice with 'start' and 'stop' argument 
slicedObjWithStartStop = slice(5, 12)

# print sliced object
print(slicedObjWithStartStop)

#print substring using slicedOject
print(str[slicedObjWithStartStop])

Output

slice(5, 12, None)
fghijkl

Example 3 – slice() with ‘start’,’stop’ and ‘step’ arguments

str = 'abcdefghijkl'

#slice with 'start', 'stop' and 'step'
slicedObjectWithStartStopStep = slice(2, 12, 2)

# print sliced object
print(slicedObjectWithStartStopStep)

#print substring using slicedOject
print(str[slicedObjectWithStartStopStep])

Output

slice(2, 12, 2)
cegik

Example 4 – slice complete string

str = 'abcdefghijkl'

#slice object for complete string
completeStringSlice = slice(0,len(str))
print(str[completeStringSlice])

Output

abcdefghijkl

Example 5 – slice complete string in reverse order

str = 'abcdefghijkl'

#slice object for complete string in reverse order
reverseStringSlice = slice(-1,-(len(str)+1), -1)
print(str[reverseStringSlice])

Output

lkjihgfedcba

Extending indexing syntax

In Python indexing syntax can be used to slice a string.

The syntax to slice string using indexing is:

str_object[start:end:step]

  • start is index to start slicing. This is optional. Default value is 0.
  • end is index to end slicing. Slicing stops at index end-1. This is optional. The default value is length of the string.
  • step is optional and means the increment between each index for slicing. Default value is 1.

Note: All these parameters are optional.

Examples

str = 'abcdefghijkl'

# slice complete string
print('str[::]=',str[::])

# slice complete string with only 'start' 
print('str[0:]=',str[0:])

# slice with only 'start'
print('str[8:]=',str[8:])

# slice with only 'end'
print('str[:18]=',str[:18])

#slice complete string with only 'end'
print('str[:]=',str[:])

#slice complete string in reverse
print('str[::-1]=',str[::-1])

#slice with 'start', 'stop' and 'end'
print('str[0:20:2]=',str[0:20:2])

Output

str[::]= abcdefghijkl
str[0:]= abcdefghijkl
str[8:]= ijkl
str[:18]= abcdefghijkl
str[:]= abcdefghijkl
str[::-1]= lkjihgfedcba
str[0:20:2]= acegik