Can You Subtract Strings in Python?

No, you can’t. But you can manipulate strings until you get to the value you want. This tutorial will show you how.

Published Categorized as Web & Code

You can’t subtract strings in Python because subtraction is the mathematical operation of taking away one numerical value from the other, and strings are not numerical values.

But if you want to manipulate strings in Python — which is what most Python beginners mean when they search for how to subtract strings — you can replace and slice them. This tutorial will show you how.

Replacing a String

One way to subtract from a string is using the replace() method:

a = "Hello, world!"
print(a)
# Prints "Hello, world!"

b = a.replace("Hello, ", "")
print(b)
# Prints "world"

Technically, the replace() method can be used to replace all occurrences of a specified phrase in a string with another. But if you replace those occurrences with nothing (""), it will replace them with nothing, as shown in the example code snippet above.

Slicing a String

Another way to subtract from a string is to slice it.

The simplest way to slice a string in Python is at a fixed position. For example, the code below slices a string called name, which contains a first name and last name, to produce the string firstName, which contains the first name only:

name = "John Doe"
print(name)
# Prints "John Doe"

firstName = name[:4]
print(firstName)
# Prints "John"

The problem with this method is that you need to know exactly where to slice.

To work around this, you can use the find() method to find the lowest index of the delimiter in your string that distinguishes one part from the other (in this case, that’s the blank space between the first and the last name):

name = "John Doe"
print(name)
# Prints "John Doe"

index = name.find(" ")

firstName = name[:index]
print(firstName)
# Prints "John"

See what we did there?

We found the index of the blank space between the first and last name, stored it in a variable called index, then referenced that variable to produce a firstName variable by slicing name at exactly the right place.

Splitting a String

Finally, instead of replacing a slicing a string, you can split its contents into a list.

To split a string’s contents, use the split() method:

name = "John Doe"
print(name)
# Prints "John Doe"

name = name.split(" ")
print(name)
# Prints "['John', 'Doe']"
print(name[0])
# Prints "John"
print(name[1])
# Prints "Doe"

The split() method splits a string into a list at every delimiter. In this case, the delimiter is the blank space between the first name and last name.

The beauty of this method is that it preserves all names, and will work in the case where somebody has more than two names:

name = "Jane Adams Doe"
print(name)
# Prints "Jane Adams Doe"

name = name.split(" ")
print(name)
# Prints "['Jane', 'Adams', 'Doe']

You can then determine the length of your list to determine the number of names the person has, for example:

name = "Jane Adams Doe"
name = name.split(" ")
numberOfNames = len(name)
print(numberOfNames)
# Prints "3"

In Summary

You can’t subtract from strings the way you can from a number. But you can use methods like replacing, slicing, and splitting to manipulate a string’s value to meet your application’s needs.

Thanks for reading this far and I hope this tutorial helped. If you have any questions or would like to share any alternative ways to achieve this with the rest of this post’s readers, leave a comment below.

By Dim Nikov

Editor of Maker's Aid. Part programmer, part marketer. Making things on the web and helping others do the same since the 2000s. Yes, I had Friendster and Myspace.

Leave a comment

Your email address will not be published. Required fields are marked *