Home > Articles > Open Source > Python

  • Print
  • + Share This
This chapter is from the book

Math and Comparison

Just as with numbers, you can perform certain kinds of math on strings as well as compare them. Not every operator works, though, and some of the operators don’t work as you might expect.

Adding Strings Together

Strings can also be added together to create new strings. Python will simply make a new string out of the smaller strings, appending one after the next.

In the following example, we take the strings stored in two variables (in this case, someone’s first name and last name) and print them out together:

>>> first_name = "Jacob"
>>> last_name = "Fulton"
>>> first_name + last_name
'JacobFulton'

Note that Python doesn’t add any space between the two strings. One way to add spaces to strings is to add them explicitly to the expression.

Let’s add a space between the user’s first and last names:

>>> first_name + " " + last_name
'Jacob Fulton'

Multiplication

You can do some funny things with multiplication and strings. When you multiply a string by an integer, Python returns a new string. This new string is the original string, repeated X number of times (where X is the value of the integer).

In the following example, we’re going to multiply the string ‘hello’ by a few integers. Take note of the results.

>>> s = 'hello '
>>> s * 5
'hello hello hello hello hello'
>>> s * 10
'hello hello hello hello hello hello hello hello hello hello '
>>> s * 0
''

What happens if we store an integer in a string?

>>> s = '5'
>>> s * 5
55555

Normally, if we multiplied 5 by 5, Python would give us 25. In this case, however, '5' is stored as a string, so it’s treated as a string and repeated five times.

There’s some limitations to string multiplication, however. Multiplying by a negative number gives an empty string.

>>> s = "hello"
>>> s * -5
''

Multiplying by a float gives an error:

>>> s * 1.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module> TypeError: can't multiply sequence by
non-int of type 'float'

Comparing Strings

It’s possible to compare strings just as you would numbers. Keep in mind, however, that Python is picky about strings being equal to each other. If the two strings differ, even slightly, they’re not considered the same. Consider the following example:

>>> a = "Virginia"
>>> b = "virginia"
>>> a == b
False

Although a and b are very similar, one is capitalized and one isn’t. Because they aren’t exactly alike, Python returns False when we ask whether they are alike.

Whitespace matters, too. Consider the following code snippet:

>>> greet1 = "Hello "
>>> greet2 = "Hello"
>>> greet1 == greet2
False

greet1 has a space at the end of its string whereas greet2 does not. Python looks at whitespace when comparing strings, so the two aren’t considered equal.

Operators That Don’t Work with Strings

In Python, the only operators that work with strings are addition and multiplication. You can’t use strings if you’re subtracting or dividing. If you try this, Python will throw an error and your program will stop running.

>>> s = "5"
>>> s / 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'int'

If you ever see an error like this one (unsupported operand type), it usually means that the data type you’re trying to use doesn’t know how to use that operator.

  • + Share This
  • 🔖 Save To Your Account