Lesson 9: String Data Structure in Python

Learn python programming in 12 lessons


Strings in Python behave just the same way as strings in other programming languages. They are made up of any character enclosed in single, double or triple quotes. These are all regarded as string

'This is PYTHON!'
"This is PYTHON!"
"""This is PYTHON!"""
The last string in triple quotes is called a doc string and it can be accessed from the python shell using the expression print(function name.__doc__). So when you want to include information in your functions that can help programmers understand what the function does, you should put them in a triple quote.

Strings are indexed starting at 0 to the length of the string. The length of a string is given by the function len(string) and this function loos something like this


def length(s):
    """This function returns the length of a string"""
    l = 0
    for i in s:
        l += 1
    return l # equivalent to print(l)

Evaluating the function in the Python shell produces
>>>length('crazy') or length("crazy")
      5
>>>print(length.__doc__)
      This function returns the length of a string

0 1 2 3 4 <— index
c r  a z y <— characters

-5 -4 -3 -2 -1 <— index
c    r   a  z   y <—characters

The line for i in s in the function above is traversing the string starting at index 0 to the end of the string which corresponds to length(s)-1. Take note that when a string has length n, its index go from 0 to n-1. Take note that strings can also be accessed in reverse starting at the end and counting backwards. The index n-1 corresponds to index -1 and index 0 corresponds to -n where n is the length of the string.


Because strings are used a lot in Python, they have a well developed class that has so many useful built-in functions such as

S.capitalize() —> Return a capitalized version of S
S.isalpha() —> Return True if all characters in S are alphabetic
S.isdigit() —> Return True if all characters in S are digits
S.find() —> Return the lowest index in S where substring sub is found
S.lstrip() —> Return a copy of the string S with leading whitespace removed.
S.rstrip() —> Return a copy of the string S with trailing whitespace removed.
S.split() —> Return a list of the words in S

To see the rest of built in string functions, type help(str) in your Python shell. To view the doc string of a specific string function like count, use the expression print(str.count.__doc__).

The functions found in the string class str are not the only things that can be done on a string, you might want a function that reverses the order of the string or one that returns a section of the it called a substring. You will have to write your own functions because those functions are not built in.
To be able to get a substring of a function, you need to know of a very useful functionality called slicing. slicing involves using any of these three parameters s[start,end,skips]
start refers to the starting index
end refers to the ending index
skips refers to the number of indexes to be examined
Look at this slicing example that uses string s = “crazy"

s[0] —> 'c'
s[len(s)-1] —> 'y'
s[-1] —> 'y'
s[2] —> 'a'
s[-2] —> ‘z'
s[:] —> 'crazy'
s[0:1] —> 'c'
s[0:4] —> 'craz'
s[-3:-1] —> ‘az’
s[0:4:2] —> ‘ca’
s[0:5:2] —> ‘cay’

There are three ways of reversing a string in Python by slicing it
s[-1::-1] —> 'yzarc'
s[len(s)::-1] —> 'yzarc'
s[::-1] —> ‘yzarc'

The first and last method are preferred because they don’t require you to calculate the length, you simply just access the last index in reverse (-1).

Here is a function to reverse a Python string by slicing it


def reverse(s):
   
    return s[::-1]

Share your thoughts