Python String Operations: Concatenation, Multiplication, Indexing and Slicing

Python String Operations: Concatenation, Multiplication, Indexing and Slicing

Did the excitement of the print() function wear off? There’s a lot more to strings than simply printing them out in IDLE. In this tutorial we will cover Python string operations: concatenation, multiplication, indexing and slicing. If you’re just joining us, you might want to check out our previous tutorial introducing strings.

String Operations

Think of strings like beads on a necklace. In fact, let’s make a string necklace right now. First, we need an empty string. We create strings by wrapping quotation marks around a value. I’m calling my string  necklace and I’m declaring it with two quotation marks with nothing in-between.

Now, when I call necklace…

The interpreter returns an empty string. Even though there is nothing to display, an empty string is still a string. See for yourself…

And it still holds a place in memory (remember: your memory location will be different than mine):

A blank space is considered a character so a single space is also a string. Try it:

Let’s add our first bead. We do so using the + operator and any character so long as it is wrapped in quotation marks…

Great! Now we have one bead on our necklace, or, rather, character on our string.

The operation we performed there wasn’t addition, though. It’s what in computer programming is referred to as concatenation. Concatenation is the joining of two strings end-to-end.

But wait a minute! Why did I add  necklace to itself? Because strings are immutable, which means they can’t be changed. Unlike numerical values, a string can’t be changed after it is declared. In order to store the value of a string operation, we need to assign it to a new variable. In this case, I am assigning to  necklace the value currently stored in necklace, ‘’, an empty string, plus the value ‘O’.

Try adding another character without assigning our string value to a new variable.

That gave us the output we expected. But as you will see, it didn’t store that value. Call  necklace  again:

Let’s add another bead to our necklace with an assignment statement:

That’s more like it. But at this rate it’s going to take forever to make a nice, long string of characters. Let’s speed up the process with the * operator.

Ooh, pretty!

String Indices and Slices

Because a string is a sequence, we can access a single character or slices of it with the index operator, [ ]. I like to think of the index operator like the claw crane game you find in an arcade. Let’s create a new string, ‘sock monkey’, assigned to the variable s , for string.

I drop my index claw into my variable, like so:

And I pull out the zeroth element, ‘s’.

Let’s try that again…

What happens if I grab the 4th element?

My index claw grabs the blank, which, though it doesn’t look like much, is still a valid and very important character.

Grabbing individual letters from a string is fun… the first few times. So let’s extract larger chunks. We can pass more than one value to the index operator, defining the range of the slice we want to extract. I want the monkey without the sock, so…

This is the same as:

If we want everything beyond our starting index, we don’t need to specify our end point.

It works in the other direction, too.

Did you notice something odd there? We declared index 4, and though our interpreter returned four characters, it only returned those from the indices 0, 1, 2 & 3, not 4. This is how slicing works in Python. The operation returns everything up to the specified index.

Let’s verify this by calling the indices individually:

We can also access indices backwards with negative integers:

Cool, right? It gets better. Next up? String methods!

Python String Operations: Concatenation, Multiplication, Indexing and Slicing was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion
  • San


    >>> s[:-2]
    ‘sock monk’
    >>> s[-3:]

    According to logic S[-3:]
    ‘ monkey’

    am I wrong ?

  • Enka

    When using negative integers does it count backwards like this: ‘(0) y(1) e(2) k(3)?

  • Enka

    I tested it and it returns ‘key’. I asked my own follow-up question above.

  • Hi San,

    I’m not sure how you arrived at ‘ monkey’ with s[-3:]. To clarify:

    >>> s[:-2]
    ‘sock monk’

    The example above can be understood as: return everything but the last two characters.

    >>> s[-3:]

    This example can be understood as: return everything after counting back three characters.

    Let me know if I can be of more help.

  • Hi Enka,
    Counting with computers is always tricky. As we’ve established, Python starts with 0. Try this:
    >>> s[0]
    >>> s[-0]

    Negative zero! What? Python must start counting somewhere, but what is negative zero?
    Try this:

    >>> s[1]
    >>> s[-1]

    Your example is right on track. When slicing a sequence, it might be useful to count the slice positions and not the items. For example:

    | s | o | c | k | | m | o | n | k | e | y |
    0 1 2…. …-2 -1 0

    When slicing an index, the first integer returns everything AFTER the slice.
    >>> s[2:]
    ‘ck monkey’
    >>> s[-2:]

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid