Strings

A string is a sequence of characters contained within a pair of single quotes (') or double quotes("). Strings can store words, sentences, or whole paragraphs. They can be any length and can contain letters, numbers, symbols, and spaces.

message1 = "I am a string"
message2 = 'I am also a string'

Other data types such as integers, doubles, and booleans can also be strings provided that they are wrapped in quotes.

All of the following are strings:

"2"
'3.6'
"True"

However, none of the following are strings:

7
Hello
True

Strings are immutable; they cannot change. Every time an operation is performed on a string, a new string is created in memory.

Accessing the Characters of a String

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

name = "phillis"
name[0] # "p"

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

name = "phillis"
name[8] # Throws an IndexError

Multi-line Strings

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 single or double quotes:

my_string = """If it were done when 'tis done, then 'twere well
It were done quickly: if the assassination
Could trammel up the consequence, and catch
With his surcease success; that but this blow
Might be the be-all and the end-all here,
But here, upon this bank and shoal of time,
We'ld jump the life to come."""

Escape Characters

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

my_string = 'It's a lovely day!'
print(my_string)

This will raise an error, because the interpreter thinks 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:

my_string = 'It\'s a lovely day!'
print(my_string)
# Output: It's a lovely day!

This problem can be avoided by wrapping strings containing ' characters in double quotes:

my_string = "It's a lovely day!"
print(my_string)
# Output: It's a lovely day!

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

Modifying Strings

Python has special operators to modify strings. For example, + can be used to concatenate strings, and * can be used to duplicate a string. The keyword in can be used to see if a given character or substring exists in a string.

string_one = "Hello, "
string_two = "World! "
combo = string_one + string_two
print(combo)
# Output: Hello, World!
new_combo = combo * 2
print(new_combo)
# Output: Hello, World! Hello, World!
if "World" in new_combo:
print("It's here!")
# Output: It's here!

Strings in Python are technically a type of list—one in which each character is a separate element in that list. This means each letter in a string can be accessed by indexing, as with the elements in a list:

Built-in String Methods

Python has a number of built-in string methods that 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 listed below.

Strings

.capitalize()
Takes in a string, and returns a copy of the string in Capital Case.
.casefold()
Returns a copy of the string with all characters in lowercase.
.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.
.find()
Takes in a substring (and optionally start/end index), return the index number of the first occurrence of the substring inside a string.
.format()
Formats the string by inserting the objects which are passed into the arguments to the placeholders in the string
.index()
Searches through a string variable for the occurrence of a pattern or a substring.
.islower()
Takes in a string and returns True if all the letters in the string are in lowercase, else returns False. Ignores spaces, newlines, numeric and special characters in the string.
.isupper()
Takes in a string and returns True if all the letters in the string are in uppercase, else returns False. Ignores spaces, newlines, numeric and special characters in the string.
.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.
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn Python on Codecademy