Python - String Sequence Operations

Introduction

As sequences, strings support operations that assume a positional ordering among items.

For example, if we have a four-character string coded inside quotes, we can verify its length with the built-in len function.

We can fetch its components with indexing expressions:

Demo

S = 'test'           # Make a 4-character string, and assign it to a name 
print( len(S) )      # Length 
print( S[0] )        # The first item in S, indexing by zero-based position 
print( S[1] )                # The second item from the left
# from   w ww. ja v a2  s .  c  om

Result

In Python, indexes are coded as offsets from the front, and so start from 0: the first item is at index 0, the second is at index 1, and so on.

In Python, we can index backward, from the end-positive indexes count from the left.

The negative indexes count back from the right:

Demo

S = 'test'           # Make a 4-character string, and assign it to a name 
print( S[-1] )       # The last item from the end in S 
print( S[-2] )       # The second-to-last item from the end
# from   w w  w . j ava2 s. c o m

Result

A negative index is simply added to the string's length, so the following two operations are equivalent:

Demo

S = 'test'           # Make a 4-character string, and assign it to a name 
print( S[-1] )       # The last item in S 
print( S[len(S)-1] ) # Negative indexing, the hard way
# from w  w  w. j  a v  a  2  s .c  om

Result

We can use an arbitrary expression in the square brackets, not just a hard-coded number literal.

Sequences support a more general form of indexing known as slicing.

It is a way to extract an entire section (slice) in a single step. For example:

Demo

S = 'test'           # Make a 4-character string, and assign it to a name 
print( S[1:3] )               # Slice of S from offsets 1 through 2 (not 3)

Result

In a slice, the left bound defaults to zero, and the right bound defaults to the length of the sequence being sliced.

This leads to some common usage variations:

Demo

S = 'test'        # Make a 4-character string, and assign it to a name 
# from  www . ja  va2s.c  o m
print(S[1:])      # Everything past the first (1:len(S)) 
                  # S itself hasn't changed 
print( S[0:3] )   # Everything but the last 
print( S[:3] )    # Same as S[0:3] 
print( S[:-1] )   # Everything but the last again, but simpler (0:-1) 
print( S[:] )     # All of S as a top-level copy (0:len(S))

Result

Related Topic