You can learn about Strings in Python Programs with Outputs helped you to understand the language better.

Python Programming – String Slices

Slicing is a mechanism used to select a range of items from the sequence. You can retrieve a slice (sub-string) of a string with a slice operation. The slicing operation is used by specifying the name of the sequence followed by an optional pair of numbers separated by a colon enclosed within [ ] (square brackets). The slice operator [ ] is used to access the individual characters of the string. However, you can use the : (colon) operator in Python to access the substring. Note that this is very similar to the indexing operation you have been using till now. Characters are accessible using their position in the string which has to be enclosed in brackets following the string.

The position number can be positive or negative, which means starting at the beginning of the end of the string. Substrings are extracted by providing values of the start and end positions separated by a colon. Both positions are optional, which means you can choose to start extracting at the beginning of the string or to extract the substring until the end. While accessing characters, it is prohibited to access a position that does not exist. During substring extraction, the longest possible string is extracted. Figure 6.16 summarizes the arrangement of a string ‘PORTION’.

Str P O R T I O N
Positive index 0 1 2 3 4 5 6
Negative index -7 -6 -5 -4 -3 -2 -1

Slicing on a string

# Slicing on a string
mystr = ‘PORTION’print(‘characters 1 to 3 is’, my_str[1:3])
print(‘characters 2 to end is’, my_str[2:])
print(‘characters 1 to -1 is’, mystr [1:-1])
print(‘characters 0 to -1 is’, my_str[:-1])
print(‘characters -7 to -1 is’, my_str[-7:-1])
print(‘characters -5 to -3 is’, my_str[-5:-3])
print(‘characters start to end is’, my_str[:])RUN
characters 1 to 3 is OR
characters 2 to end is RTION
characters 1 to -1 is ORTIO
characters 0 to -1 is PORTIO
characters -7 to -1 is PORTIO
characters -5 to -3 is RT
characters start to end is PORTION


  • [ ] is known as slice operator. It is used to access the sub-strings of a particular string.
  • [:] is known as the range slice operator. It is used to access the characters from the specified range.

Remember the following points while accessing elements in the strings using subscripts:

(a) The first number (before the colon) in the slicing operation refers to the position from where the slice starts, and the second number (after the colon) indicates where the slice will stop.

(b) If the first number is not specified, Python will start extracting characters from the beginning of the sequence.

(c) If the second number is left out, Python will stop at the end of the sequence.

(d) Note that the slicing starts at the start position and ends just before the end position, i.e., the start position is included, but the end position is excluded from the sequence slice. Thus, my_str[l:3] returns a slice of the sequence starting at position 1, includes position 2 but stops at position 3. Therefore, a slice of two items is returned.

(e) Omitting both the indices directs the Python interpreter to extract the entire string starting from 0 till the last index. Thus, my_str[:] returns a copy of the whole sequence.

(f) Positive subscript helps in accessing the string from the beginning. You can also do slicing with negative positions. Negative numbers are used for positions from the end of the sequence, i.e., count backward from the end of the string. For example, my_str[:-l] will return a slice of the sequence, which excludes the last item of the sequence but contains everything else.

(g) If the first index is greater than or equal to the second, the result is an empty string, represented by two quotation marks.

(h) Note that -0 is the same as 0, so it does not count from the right.

Let’s Try

Write the output of the following code segments:

>>> word=”language”
>>> word[1:3]
>>> word[5:]
>>> word[-l]
>>> word[-l:]
>>> word[::-1]
>>> s=”ten”
>>> rev_str = s[len(s)::-1]
>>> print(rev_str)

Let’s Try

Write the output of the following Python code.

# Slicing Python String x =”BPB PUBLICATIONS”
# Slicing the String using two indexes a = x [0 :3]
print(“Both Indexes = “, a)
# Slicing the String using Second indexes b = x[:10]
print(“No First Index = “, b)
# Slicing the String using First indexes c = x[4 : ]
print(“No Second Index = “, c)
# Slicing the String without using two indexes d = x [: ]
print(“No Indexes = “, d)
# Slicing the String using Negative indexes e = x [-3 : ]
print(“Negative First Index = “, e)
# Slicing the String using Negative indexes f = x[:-2]
print(“Negative Second Index = “, f)

Specifying a stride in a String Slice
There is one more variant of the slicing syntax. Adding an additional: and a third index designates a stride (also called a step), which indicates the number of characters to jump after retrieving each character in the slice. For example, for the string ‘PORTION’, the slice 0:7:2 starts with the first character and ends with the last character (the whole string), and every second character is skipped.
»> mystr = ‘PORTION’
>>> my_str[0:7:2]

Leave a Reply

Your email address will not be published. Required fields are marked *