String Basics
An example of a literal constant is a number like 5 , 1.23 , or a string like 'This is a string' or "It's a string!" .
What a String Is
A string is a sequence of characters. Think of it as a container that holds text—words, sentences, symbols, numbers written as text, or any combination of these. In Python, you will use strings constantly: to display messages to users, to store names and addresses, to process text data, and much more. Understanding how strings work is essential because you will be using strings in almost every Python program that you write.
When you log into a website and enter your username, that username is stored as a string. When a program displays 'Welcome, Alice!' on your screen, that entire message is a string. When you search for a word in a document, you are searching for a string. Strings are everywhere in software.
String Literals: How to Write Strings
A string literal is a string written directly in your code as a fixed value. You create a string literal by surrounding text with either single quotes or double quotes. Both work identically in Python—the choice is yours, but consistency matters.
The quotes are not part of the string itself—they are syntax that tells Python where the string begins and ends. When Python reads 'hello', it stores just hello (without the quotes) in memory.
You can specify strings using single quotes such as 'Quote me on this'. You can also use double quotes: "Quote me on this". Both produce identical strings. Choose whichever feels natural, but pick one style and stick with it within a project for readability.
String Literals vs. String Variables
It is important to understand the difference between a string literal (the actual text written in quotes) and a variable that holds a string. A string literal is a fixed value written directly in your code. A variable is a named container that can store a string (or any other value). When you assign a string literal to a variable, the variable becomes a reference to that string.
String Literal vs. String Variable
Distinguish between the string literal and the variable in this scenario: You write the code greeting = 'Hello, World!'. What is the literal? What is the variable?
Identify the string literal: The string literal is 'Hello, World!' — the text surrounded by quotes that appears directly in your code. This is a fixed value.
Identify the variable: The variable is greeting — the name on the left side of the equals sign. This is a container that holds the string.
Understand the relationship: When Python executes greeting = 'Hello, World!', it creates a variable named greeting and stores the string literal 'Hello, World!' inside it. Now you can use the name greeting to refer to that string later in your code.
The literal is the fixed text 'Hello, World!'. The variable is greeting, which holds that text.
This distinction matters because once you store a string in a variable, you can use that variable name throughout your program. You do not have to retype the string literal every time. You can also change what a variable holds by assigning a different string to it later.
Common Mistakes with Strings
Forgetting to close a quote
Python will not know where the string ends. It will either report a syntax error or keep reading until it finds a closing quote, consuming code that should be separate statements.
Fix:
Always pair opening and closing quotes: name = 'Alice'Mixing quote types without care
The string opens with a single quote but closes with a double quote. Python sees the opening single quote and looks for a closing single quote, so it treats the double quote as part of the string content and continues looking, causing an error.
Fix:
Match your quotes: message = 'Hello, World!' or message = "Hello, World!"Trying to include a quote inside a string without escaping
The single quote inside the string (in It's) closes the string prematurely. Python then tries to parse s a beautiful day' as code, which fails.
Fix:
Use double quotes around the string: quote = "It's a beautiful day" or escape the inner quote: quote = 'It\'s a beautiful day'Confusing the variable name with the string it holds
name (without quotes) is a variable—a container. 'name' (with quotes) is a string literal containing the four characters n, a, m, e. They are completely different.
Fix:
Remember: no quotes means variable; quotes mean string literal.
Why Strings Matter
Strings are one of the most frequently used data types in programming. Almost every program needs to display information to users, read input from users, or process text in some way. Mastering strings early will make it much easier to write real programs. Pay close attention to string syntax now so that you do not waste time debugging quote-related errors later.
Practice
Write a variable assignment that stores your favorite book title as a string. Then write another assignment that stores your favorite author's name. Use consistent quote style for both.
Hints
- Use the format: variable_name = 'string content'
- Pick either single or double quotes and use the same style for both variables
- Make sure your quotes are balanced (every opening quote has a closing quote)
Explain the difference between the following two lines. What will Python do with each one? (1) print('hello') and (2) print(hello)
Hints
- Think about what quotes mean in Python
- In the first line, 'hello' is a string literal. In the second line, hello is a variable name.
- What happens if you try to use a variable that does not exist?
Key Takeaways
- A string is a sequence of characters—text data that you use in almost every Python program.
- String literals are created by surrounding text with single or double quotes; both are valid and produce identical results.
- The quotes are syntax that tell Python where the string begins and ends; they are not part of the string itself.
- A string literal is a fixed value written in your code; a variable is a named container that can hold a string.
- Always match your opening and closing quotes, and be consistent with your choice of single or double quotes throughout your code.