In Post #9, we explored the world of numbers with integers and floats. These are essential for any kind of calculation, but programs aren’t just about crunching numbers. They need to communicate with people, process textual information, and handle human language. This is where Python’s string data type comes in.
In this post, we’ll take a deep dive into strings. You’ll learn what they are, how to create them, and the important and practical difference between using single and double quotes.
What is a String?
A string is a sequence of characters enclosed in quotes. In Python, a string can be a single letter, a word, a sentence, or even an entire paragraph. If it’s text, it’s a string. The Python name for this data type is str
.
Creating a string is as simple as assigning text to a variable, which we first saw in Post #8.
# A few examples of strings
greeting = "Hello, Python!"
user_name = 'Alice'
empty_string = ""
Creating Strings: Single Quotes vs. Double Quotes
Python is flexible when it comes to creating strings. You can use either single quotes ('
) or double quotes ("
). Functionally, there is no difference between them; they both create a string object.
string_a = 'This is a string.'
string_b = "This is also a string."
print(string_a)
print(string_b)
So, why are there two options? The reason is convenience. The ability to choose your quotes makes it incredibly easy to include a quote character within the string itself without causing an error.
Including Quotes Inside Strings
Let’s say you want to create a string that contains an apostrophe, like “It’s a sunny day.” If you try to wrap it in single quotes, Python will get confused and raise a SyntaxError
.
To include a single quote (like an apostrophe), you should wrap the entire string in double quotes. This way, Python knows the single quote inside is part of the text.
sentence = "It's a sunny day."
print(sentence)
Similarly, to include double quotes for a quotation, you can wrap the string in single quotes.
quote = 'He said, "Python is amazing!"'
print(quote)
The Escape Hatch: Using the Backslash
What if you need to use the same quote character on the inside and the outside? Python provides a solution with the backslash (\
), which is known as an escape character.
An escape character tells Python that the character immediately following it should be treated as a literal character, not as part of the code’s syntax.
# Escaping a single quote
sentence = 'It\'s a sunny day.'
print(sentence)
# Escaping double quotes
quote = "He said, \"Python is amazing!\""
print(quote)
A Quick Look at Multi-Line Strings
As we first saw in Post #7 when we discussed comments and docstrings, Python also allows you to create strings that span multiple lines. You can do this with triple quotes, either """
or '''
.
This is useful for storing long blocks of text where the line breaks are part of the content.
long_message = """Dear user,
This is a multi-line string.
All the line breaks and spacing are preserved.
Sincerely,
Your Python Program
"""
print(long_message)
What’s Next?
You now know what a string (str
) is and how to create one using single, double, or even triple quotes. Understanding how to manage quotes within your strings is a fundamental skill that you will use constantly when working with text.
We’ve now covered numbers and text, two of the most common data types. But to make decisions in our programs, we need one more fundamental building block: a way to represent truth and falsehood. In Post #11, we will explore the boolean data type, which is the simple yet powerful foundation of all logic in programming.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com