Learning Management Platform for Written Tutorial Series.

Python 3 Strings - Learn how to work with string data types

Python 3 Strings - Learn how to work with string data types

Python Strings

Strings are some of the most commonly used data types in python. They can contain characters, numbers and symbols. Creating string in python is super easy. In this tutorial, we will explore strings in python and look at some of the built-in functions that make it really supper easy to manipulate strings,

Topics to be covered

We will cover the following topics in this lesson.

  • String variables
  • String operations
  • String functions
  • String slicing

String variables

In this section, we will look at three (3) ways of creating strings. We will assume that you are working on an application that needs to process web content. Our application will have a title, description and content section.

We can do it like so

title = 'Zen Fire'
description = "Zen, the master's way of life"
content  = """We seldom realize, for example that our most private thoughts and 
emotions are not actually our own. For we think in terms of 
languages and images which we did not invent, but which were 
given to us by our society - Alan Watts.
"""

print(title)
print(description)
print(content)

HERE,

  • title = 'Zen Fire' - defines a string variable
  • description = "Zen, the master's way of life" - defines a string variable that contains a special character ' but the interpreter does not raise an error because we defined our string using " double quotes so the single quote is automatically escaped for us.
  • """We seldom...""" defines a string with multiple lines and because with used triple double quotes to define it the interpreter does not raise any errors. The new line feeds are automatically escaped for us.

When dealing with strings, you can choose single or double quotes but if the string will contain a single quote then use double quotes. If it will contain a single quote then use double quotes.

Let's assume you swore to the gods to always define strings using single quotes and you want to include the single quote as part of the string. You can do it like so

title = 'Zen\'s fire'

HERE,

  • \' the backslash escapes the single quote ' so that it is treated as part of the string value and not ending the string value.

String Operations

In this section, we will look at some of the operations that we can perform using strings. Lets start with concatenation

given_name = "John"
surname = "Doe"

print (given_name + " " + surname)

However, we can only use the addition operator when working with values that are all of string type. Try to add an int or float to a string raises an error.

Consider the following code

print ("3" + 3)

Executing the above code produces the following error

Traceback (most recent call last):
  File "ops.py", line 5, in <module>
    print ("3" + 3)
TypeError: can only concatenate str (not "int") to str

HERE,

  • given_name + " " + surname joins the given_name and surname separated by a space. We use the plus + sign to join strings.

We can also use the multiplication sign to repeat a string a specific number of times.

monty = "spam eggs sausage "

print(monty * 3)

HERE,

  • print(monty * 3) prints the value of the variable monty three (3) times.

Executing the above code produces the following results

spam eggs sausage spam eggs sausage spam eggs sausage

Python string functions

In this section, we will deal with some of the functions that operate on strings.

Let's say we want to get the length of a string, we can do it like so.

monty = "spam eggs sausage "

print(len(monty))

HERE,

  • len(monty) we are using a python built-in function len that accepts the parameter monty and it returns the length of the string.

The above code produces the following result

18

Remember that variables are actually objects so we can call the string object built-in methods directory.

Let's say we want to print the value of our variable in all lower case letters. we can call the lower method like so

monty = "Spam eggs sausage"

print(monty.lower())

We get the following when we execute the above code

spam eggs sausage

Let's now turn all letter into capital letters

monty = "Spam eggs sausage"

print(monty.upper())

The above code produces the following

SPAM EGGS SAUSAGE

We can also capitalize the first letter of each word like so

monty = "Spam eggs sausage"

print(monty.title())

The above produces the following

Spam Eggs Sausage

String formatting

Concatenation is good but it looks weird. Python provides us with a better way of doing things. Let's revisit the code we wrote above

given_name = "John"
surname = "Doe"

print (given_name + " " + surname)

we can refactor the above code to the following

given_name = "John"
surname = "Doe"

print ("{} {}".format(given_name,surname))

HERE,

  • print ("{} {}".format(given_name,surname)) uses place holders {},{} in our string then calls in the format method of the string object and passes in the given_name,surname variables as parameters to the function. The parameters are implicitly assigned to the placeholders in the order that they appear.

In the above example, we let python assign values in the order that they appeared. In this next example, we will give our placeholders index that so that we have control over were we place our values.

Modify the code as follows

given_name = "John"
surname = "Doe"

print ("{1}, {0}".format(given_name,surname))

HERE,

  • print ("{1}, {0}".format(given_name,surname)) the place holders have been reversed, we have {1}, which is the second parameter coming first followed by a comma and the first parameter index {0} has been placed second. The above code will produce the following results
Doe, John

In the above example, we had control over the place holders but not the order of the parameters. The good news is that python allows us to have full control over the order of the placeholders and parameters using keywords.

Modify the above code to the following

given_name = "John"
surname = "Doe"

print ("{last}, {first}".format(last=surname,first=given_name))

HERE,

  • {last}, {first} uses named parameters as placeholders and when assigning values we are using explicit placeholder names so the order of the parameters doesn't really matter anymore.

The above code still produces the following results

Doe, John

We can also format strings using the following syntax

given_name = "John"
surname = "Doe"

print (f"{given_name}, {surname}")

HERE,

  • f before the string value tells python to format our string and replace the variable names given_name and surname with the actual values.

The above code would produce the following results

John, Doe

We can also convert numbers to string then concatenate them the normal way string the str function like so

sum = 2500 * 3

print("The sum is " + str(sum))

HERE,

  • The str function accepts a number as a parameter then converts it to a string so doing something like "The sum is " + str(sum) does not raise an error.

Strings as lists

Python treats strings like they are a list and each item is represented using the letters and index from 0.

consider the following code

brian = "He is not the messiah"

for letter in brian:
    print(letter)

HERE,

  • for letter in brian: loops through each letter in our variable brian and prints the value in the console

The above code produces the following results

H
e
 
i
s
 
n
o
t
 
t
h
e
 
m
e
s
s
i
a
h

We can also access the characters in a string using index numbers like so

brian = "He is not the messiah"

print(brian[3])

Remember that the index numbers start from zero (0). Using the code above, the letter H is assigned an index value of 0, 1 for e, 2 for the space and 4 for the letter i and so on and so forth.

Executing the above code produces the following results

i

Let's now look at string slicing. We can slice off a part of the string that we need. For example, let's say that we want to get the first 7 letters of the variable named brian. We can do it like so

brian = "He is not the messiah"


print(brian[:5])

HERE,

  • brian[:5] gets the first 5 letters of the value stored in the variable brian. Notice we have a we nothing before the full colon : so python defaults to 0 and gets the 5 characters start from the first letter.

Let's now modify the code to the following and start from index 7 and get 3 letters only.

brian = "He is not the messiah"
print(brian[6:9])

HERE,

  • brian[6:9] skips the first 6 characters and returns the characters from position 7 to 9. The above code produces the following results.
not

We can also use a negative index to pick the last letter like so

brian = "He is not the messiah"

print(brian[-1])

We can also reverse the letters in a string like so

brian = "He is not the messiah"

print(brian[::-1])

HERE,

  • The above code picks all the letter from the right start with the last letter and returning everything that is on the left.

The above code produces the following results

haissem eht ton si eH

We can also split a string into a list using the split function like so

brian = "He is not the messiah"

word_list = brian.split(' ')

print(word_list)

HERE,

  • brian.split(' ') splits all the words in the string separated by a space into list.

Executing the above code produces the following results

['He', 'is', 'not', 'the', 'messiah']

We can also convert a list into a string like so

word_list = ['He', 'is', 'not', 'the', 'messiah']

print (' '.join(word_list))

HERE,

  • ' '.join(word_list) joins all the words in the list using a space as specified by ' '.

The above code produces the following results

He is not the messiah

Lesson Bonus

In this section, we will create a simple function that analyzers a string text and returns the word density.

The following code

def word_count(text,word):
    count = 0
    for w in text:
        if w == word:
            count += 1
    return count

text = "That's Judith, Mum. Judith, Mother."

word_list = text.split(' ')
word = 'Judith,'

print(f'The word "{word}" appears {word_count(word_list,word)} times in the text "{text}"')
percentage = 100 * word_count(word_list,word) / len(word_list)
print(f'The keyword density of "{word}" is {percentage}%')

produces the following results

The word "Judith," appears 2 times in the text "That's Judith, Mum. Judith, Mother."
The keyword density of "Judith," is 40.0%

Lesson challenge

Use the comments section below to explain how and why the above code works. It doesn't matter of you are not the first person to comment. Just explain the code and we can all learn from you. Congratulations on taking action to comment.

Modify the above code to show the density of each letter in the string then post your solution in the comments section.

Summary

A string is a list of characters that can be alphanumeric. Strings are some of the most commonly used data types. You can define strings using single or double quotes. You can also use tripple double quotes to automatically escape new line feeds. Since a string is a list in python, you can apply functions that you would normally perform on lists.

What next?

If you enjoyed this lesson then show us your appreciation by creating a free accounts on our site. As always we appreciate your comments down below.


...