Python String: Working With Text

So we’ve seen numbers, but what about text? This page is about the Python string, which is the go-to Python data type for storing and using text in Python. So, in Python, a piece of text is called a string and you can perform all kinds of operations on a string. But let’s start with the basics first!

What is a Python string?

The following is a formal definition of what a string is:

A string in Python is a sequence of characters

In even simpler terms, a string is a piece of text. Strings are not just a Python thing. It’s a well-known term in the field of computer science and means the same thing in most other languages as well. Now that we know what a string is, we’ll look at how to create a string.

How to create a Python string

A Python string needs quotes around it for it to be recognized as such, like this:

>>> 'Hello, World'
'Hello, World'

Because of the quotes, Python understands this is a sequence of characters and not a command, number, or variable.

And just like with numbers, some of the operators we learned before work on Python strings too. Try it with the following expressions:

>>> 'a' + 'b'
>>> 'ab' * 4
>>> 'a' - 'b'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'str'

This is what happens in the code above:

  • The plus operator glues two Python strings together.
  • The multiplication operator repeats our Python string the given number of times.
  • The minus operator doesn’t work on a Python string and produces an error. If you want to remove parts of a string, there are other methods that you’ll learn about later on.

Single or double quotes?

We’ve used single quotes, but Python accepts double-quotes around a string as well:

>>> "a" + "b"

Note that these are not two single quotes next to each other. It’s the character that’s often found next to the enter key on your keyboard. You need to press shift together with this key to get a double quote.

As you can see from its answer, Python itself seems to prefer single quotes. It looks more clear, and Python tries to be as clear and well readable as it can. So why does it support both? It’s because it allows you to use strings that contain a quote.

In the first example below, we use double-quotes. Hence there’s no problem with the single quote in the word it’s. However, in the second example, we try to use single quotes. Python sees the quote in the word it’s and thinks this is the end of the string! The following letter, “s”, causes a so-called syntax error: your language syntax is not correct, and Python doesn’t know what to do next and fails with an error:

>>> mystring = "It's a string, with a single quote!"
>>> mystring = 'It's a string, with a single quote!'
  File "<stdin>", line 1
    mystring = 'It's a string, with a single quote!'
SyntaxError: invalid syntax

As you can see, even the syntax highlighter on the code block above gets confused! And as you can also see, Python points out the exact location of where it encountered the error. Python errors tend to be very helpful, so look closely at them and you’ll often be able to pinpoint what’s going wrong.

There’s actually another way around this problem, called escaping. You can escape a special character, like a quote, with a backward slash:

>>> mystring = 'It\'s an escaped quote!'
>>> _

You can also escape double quotes inside a double-quoted string:

>>> mystring = "I'm a so-called \"script kiddie\""
>>> _

So which one should you use? It’s simple: always opt for the option in which you need the least amount of escapes because they make your Python strings less readable.

Multiline strings

Python also has a nice syntax for creating multiline strings, using triple quotes:

>>> my_big_string = """This is line 1,
... this is line 2,
... this is line 3."""
>>> _

The nice thing about this, is that you can use both single and double quotes without a multiline string. In fact, you can use the syntax to cleanly create strings that contain both of these:

>>> line = """He said: "Hello, I've got a question" from the audience"""
>>> _

String operations

Strings come with a number of handy, built-in operations you can execute. I’ll show you only a couple here, since I don’t want to divert your attention from the tutorial too much.

In the REPL, you can use auto-completion. In the next code fragment, we create a string, mystring, and on the next line we type its name followed by hitting the <TAB> key twice:

>>> mystring = "Hello world"
>>> mystring.
mystring.capitalize(    mystring.find(          mystring.isdecimal(     mystring.istitle(       mystring.partition(     mystring.rstrip(        mystring.translate(
mystring.casefold(      mystring.format(        mystring.isdigit(       mystring.isupper(       mystring.replace(       mystring.split(         mystring.upper(        mystring.format_map(    mystring.isidentifier(  mystring.join(          mystring.rfind(         mystring.splitlines(    mystring.zfill(
mystring.count(         mystring.index(         mystring.islower(       mystring.ljust(         mystring.rindex(        mystring.startswith(
mystring.encode(        mystring.isalnum(       mystring.isnumeric(     mystring.lower(         mystring.rjust(         mystring.strip(
mystring.endswith(      mystring.isalpha(       mystring.isprintable(   mystring.lstrip(        mystring.rpartition(    mystring.swapcase(
mystring.expandtabs(    mystring.isascii(       mystring.isspace(       mystring.maketrans(     mystring.rsplit(        mystring.title(

If all went well, you should get a big list of operations that can be performed on a string. You can try some of these yourself:

>>> mystring.lower()
'hello world'
>>> mystring.upper()

Getting the string length

A very common operation is to get the string length. Unlike the operations above, this can be done with Python’s len() function like this:

>>> len("I wonder how long this string will be...")
>>> len(mystring)

In fact, the len() function can be used on many objects in Python, as you’ll learn later on. If functions are new to you, you’re in luck, because our next page will explain exactly what a function in Python is, and how you can create one yourself.

About the author

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python!