In Python textual data is represented and stored as strings.

Strings are created with either single or double-quotes, both will be accepted in Python, but as a preference, I will be using double-quotes.

Remember we create a variable, then assign a string value using the (=) sign.

We also have triple quotes, that we use for formatting a long string such as sentences and paragraphs.

String Indexing

In Python like other programming languages, strings are indexed at 0.

First let’s determine the length of a string.

If we want to know how many characters we have in a string we use the len function.

A function is a block of code we can use multiple times, makes our lives as programmers simpler and len is short for length.

From our examples above we see our string “Thato Kubheka” has 13 characters.

If we want specific characters from our string we use square bracket notation [].

From name our character at index zero is T, the beginning of the string, as strings are indexed at 0.

In the example above we have asked Python to access the third character in the string, so it will count from zero and stop at 3, which outputs the small letter t.

We can also use negative index, and in the examples below we will be using  -3 as our negative index.

A negative index starts from the end of the string, so it will count 3 characters from the end of the string, which will output the letter e.

String Slicing

Let’s extract the first 5 characters from our string;

The index of the above character are 0,1,2,3,4, meaning the character at the end is not included.

We can also omit the start and end index in our string;

In the example above we have omitted the first index which is 0 in our string, the output returned is Thato, the same output as our previous example.

Now let’s omit the last index which is 5.

We have omitted the last index which is 5,and kept the beginning one which is 0.

The output returned is the entire string name Thato Kubheka.

Escape Sequences

Let’s create a new variable called message and assign it a string value, this time using single quotes.

When we run the program we get an error, this is because the Python interpreter sees the character Hi its Ben as the as the quotes of the string, this the rest of the code is meaningless and invalid.

There are two ways to fix this, the first solution is using double quotes;

The other method is using a backslash character(\), which is known in Python as an escape character.

We use escape characters to escape the character after.

So we put the backslash before the single quote, this backslash (\) is our escape character and backslash single quote(\’) is our escape sequence.

Let’s say we want to add a backslash to our string, how would we go about it?

So if we want to include a backslash in our string, we just prefix it with another backslash (\\).

The last of our escape sequences is \n, which is used for creating new lines in Python.


This is a process of combining two or more strings into one.

The process of asking Python to print the first_name and last_name and separating them by a comma is totally ok, but there is an easier way, known as concatenation.

We simply create a variable full_name and assign the strings first_name and last_name as the values using plus signs(+).

Formatted Strings

If you do not want to use concatenation, then formatted strings is another option.

When applying the formatted strings methodology we use curly brackets and before the curly brackets we insert an f, which can be upper case (F) or lower case (f).

So at runtime the expression will be evaluated, and the values in between the curly brackets will be replaced.

You can put any valid expression in between the curly brackets.

String Methods

In Python we have functions that are specific to strings, these functions are also referred to as methods, print and len are general-purpose functions and are not specific to strings thus are not classified as methods.

We use the .operator to access these multiple function such as .upper, .lower, .casefold, .centre etc.

Homework : play around with all the sting methods you can find on the internet.