Strings

Strings

A string is a sequence of characters contained within a pair of single quotes (') or double quotes("). They provide a way to store something like a word, sentence, or whole paragraph. They can be any length and can contain letters, numbers, symbols, and spaces.

Loading...

Other data types such as integers, doubles and booleans, can also be strings as long as they are wrapped in quotes:

All of the below are strings:

Loading...

All of the below are not strings:

Loading...

Strings are immutable, or cannot change. Each time we perform an operation on a string, it will create an entirely new string.

Accessing Characters of a String

A string can be thought of as a list of characters. Each character has an index, starting from 0.

Loading...

If you try to access an index out of bounds, it will return an IndexError.

Loading...

Multi-line String

Strings can be long or short. If your text is long, you may want to use a multi-line string. Multi-line strings begin and end with three double or single quotes:

Loading...

Escape Character

Sometimes a string may have a character that Python tries to interpret such as '.

Loading...

This will give an error because the interpreter thinks that the second ' marks the end of the string.

  File "main.py", line 1
    my_string = 'It's a lovely day!'
                    ^
SyntaxError: invalid syntax

These characters can be “escaped” by adding a backslash beforehand. The \ is called an escape character.

The backslash will not be visible if the string is printed:

Loading...

Python also has a series of non-printable characters that can modify strings. For example, \n will add a new line, and \t will add a tab:

Modifying Strings

Python has special operators to modify strings. For example, we can use + to concatenate two strings or * to duplicate a string. We can also use the keyword in to see if a given character exists in a string.

Loading...

Strings in Python are technically a type of a list, where each character is a separate element in that list. That means we can access different letters in our string the same way we can access items in a list:

Built-in String Methods

Python has many built in string methods to manipulate strings. However, when these methods are called, the original string will not be changed, so any modifications will need to be saved to a new variable. A few useful built-in string methods are:

Strings

.capitalize()
Takes in a string, and returns a copy of the string in Capital Case.
.casefold()
Takes in a string, and returns a copy of the string with all characters in lower case.
.count()
Takes in a list of values of any data type, and returns the number of times(count) a particular value is present within the list.
.lower()
Takes a string, and returns a copy of that string in which all letters are lowercase. Numbers and symbols are not changed.
.partition()
Searches a string for a given keyword and splits that string into a three part tuple.
.replace()
Replace a specific substring with another substring.
.split()
Converts a string to a list. It takes a specified delimiter and a maximum number of items to split as optional parameters.
.swapcase()
Takes a string and returns a copy of that string in which all lowercase letters are uppercase, and all upercase letters are lowercase. Numbers and symbols are not changed.
.title()
Takes in a string and returns a copy of the string formatted in the title case: each word in the string is capitalized.
.upper()
Takes a string, and returns a copy of that string in which all letters are lowercase. Numbers and symbols are not changed.
Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn Python on Codecademy