Skip to main content

Strings

To create a string in Python, wrap your text in quotation marks. Single and double quotes both work, so 'hello' and "hello" give you the very same value.

The print() function shows a string literal on the screen.

Example

Prompt
Write a Python program that prints "Hello" using double quotes and single quotes.
Output: complete Python program

Quotes Inside Quotes

A string may contain quote characters, but they have to be a different kind from the ones wrapping the string.

Example

Prompt
Write a Python program that demonstrates using quotes inside strings by mixing single and double quotes.
Output: complete Python program

Assign String to a Variable

To store a string in a variable, write the variable's name, then an equal sign, then the string.

Example

Prompt
Write a Python program that assigns a string to a variable and prints it.
Output: complete Python program

Multiline Strings

Three quotes in a row let you store a string that spans several lines in a variable.

Note: The output keeps each line break exactly where it sits in your code.

Example

Three double quotes do the job:

Prompt
Write a Python program that assigns a multiline string using three double quotes and prints it.
Output: complete Python program

Example

So do three single quotes:

Prompt
Write a Python program that assigns a multiline string using three single quotes and prints it.
Output: complete Python program

Strings are Arrays

As in many other languages, a Python string is essentially an array of Unicode characters.

There is no separate type for a single character, though. One character is just a string whose length is 1.

To reach an element of a string, put its position in square brackets.

Example

Fetch position 1 from the string. Positions are counted from 0, so this gives us the second character:

Prompt
Write a Python program that accesses a character at position 1 of a string using square bracket notation.
Output: complete Python program

Looping Through a String

Because a string works like an array, a for loop can visit its characters one at a time.

Example

Print each letter of "banana" on its own line:

Prompt
Write a Python program that loops through the letters in the word "banana" using a for loop and prints each character.
Output: complete Python program

String Length

The built-in len() function tells you how long a string is.

Example

Here, len() counts the characters and gives back 13:

Prompt
Write a Python program that prints the length of the string "Hello, World!" using the len() function.
Output: complete Python program

Check String

The keyword in tells you whether a particular phrase or character appears somewhere inside a string.

Example

Look for "free" in this sentence:

Prompt
Write a Python program that checks if the word "free" is present in a string and prints the result.
Output: complete Python program

Example

Combine it with if to print only when "free" is found:

Prompt
Write a Python program that uses an if statement to check if "free" is in a string and prints a confirmation message.
Output: complete Python program

Check if NOT

To confirm that a phrase or character is absent from a string, use not in instead.

Example

Confirm that "expensive" does not appear in this sentence:

Prompt
Write a Python program that checks if "expensive" is NOT present in a string and prints the result.
Output: complete Python program

Example

Print a message only when "expensive" is missing:

Prompt
Write a Python program that uses an if statement to check if "expensive" is NOT in a string and prints a message.
Output: complete Python program