String Operators

You may have already seen operators applied to numeric operands. In this lesson, you’ll see how operators can be applied to strings as well. Here are the operators you’ll practice using in this lesson:

  • The + operator concatenates strings:
>>> s = 'spam'
>>> t = 'egg'
>>> u = 'bacon'

>>> s + t
>>> s + t + u
  • The * operator creates multiple copies of a string:
>>> s = 'spam'
>>> n = 2

>>> s * n

>>> s = 'spam.'
>>> n = 8
>>> s * n
>>> s = 'spam'
>>> s in 'I saw spamalot!'
>>> s in 'I saw The Holy Grail!'

>>> s not in 'I saw The Holy Grail!'
>>> s not in 'I saw spamalot!'

To learn more, check out Operators and Expressions in Python. Here are a few resources on using the REPL (Read Eval Print Loop):

00:00 First off, I’ll show you how operators work with strings. A couple of the operators that you use on numeric operands can be applied to strings as well—the operator that looks like a plus sign (+), which in this case is considered the concatenation operator, and the multiplication sign operator (*), which with strings is considered the replication operator.

00:22 Then I’ll show you a membership operator that can be used with strings as well. It’s the in operator. So to start, the + operator concatenates strings.

00:34 It returns a string consisting of the operands joined together. Let me show you what that looks like. For these examples, I’m going to be using a custom REPL, so instead of just typing python or python3 and entering into the standard REPL, I’m going to be using one named bpython. There will be a link in the text below this video that can give you more information about installing it if you’d like to use it yourself.

00:59 You’ll see that it behaves a little differently than the standard REPL. And you can see that I’m using Python 3.7. Okay. To talk about the concatenation operator, first, you need a couple strings.

01:13 So I’m going to have you create a few. I’m going to use the Monty Python metasyntactic variables instead of foo and bar and baz, because those are kind of hard to pronounce and these are a little bit more fun and a little bit easier to use.

01:26 So, the first string is going to be s = 'spam'. t, the next string object you’ll create, will be 'egg',

01:37 And u will be a string of 'bacon'. So s, t, and u—three strings. So, what happens if you use the operator + to have s + t? Well, that concatenates them into a single string.

01:55 What if you combine all three? s + t + u, it will concatenate all three and get 'spameggbacon'. It simply just joins the operands together.

02:07 You could use it in a print() statement, you could have two strings inside of here and just put it inside the parentheses. So the string 'Go team' is added to the string with three exclamation points.

02:19 So, that’s the concatenation operator. Let’s look at the next one. The multiplication operator, which is an asterisk (*), creates multiple copies of a string.

02:29 It’s going to return a string consisting of n number of concatenated copies of a string. Let me show you what that looks like. I’ll have you keep using the object s that you created a moment ago.

02:40 s is, again, 'spam'. I’ll have you create an object n, which is an integer. n = 2. What happens if you multiply s * n? That operator replicates the string object twice and concatenates them together, so you get multiple copies.

03:01 You could do, let’s say, n = 8. So with s * n, you get a lot of 'spam'.

03:08 Even if you have other characters in there—you modify s a little bit and make it have a period ('.') in it. Now if you multiply s * n, you’ll see that it has a period in between each one—or at the end of each one. So the multiplier, this other operand of n, needs to be an integer. It won’t work if you say s * 1.2.

03:28 You can’t multiply a sequence by a non-integer. You can’t use a float. And you could just simply type something as 'bacon', as a string, * the number as we did before. What if it’s a negative number? Say, -7?

03:44 That will return an empty string. It’s not required that you have a positive integer,

03:51 but if you use zero or a negative number, it’s just going to result into an empty string. The in operator is a membership operator that can be used with strings.

04:01 It’s going to return True if the first operand is contained within the second, and it’ll return False, otherwise. So, try it out. To try this out, I’ll have you recreate the string object 'spam', s = 'spam'. And then to write this out, you’ll say s in 'I saw spamalot!', and it’ll say True.

04:28 But otherwise, if s in 'I saw The Holy Grail!', it would return False. Another way that you can use it is also as not in—that the contents of the first string object are not contained within the second.

04:45 I’ll have you rewrite that as s not in 'I saw The Holy Grail!'.

04:56 Or you could just say this string,

05:01 which would be False because it is in there. 'I saw' is part of the string 'I saw spamalot!'. in and not in provide a handy way to check if one operand is contained within another, hence why they’re called a membership operator.

05:16 Next, I’ll have you dive into built-in Python string functions.

Become a Member to join the conversation.