In this tutorial, I will explain how to check if a string is surrounded by quotes in Python. As a Python developer working on a project for one of my USA clients, I needed to check if a string was surrounded by quotes. We’ll cover different methods to achieve this and provide detailed examples to make the concepts clear and relatable.
Methods to Check if a String is Surrounded by Quotes
There are several ways to check if a string is surrounded by quotes in Python. We’ll explore all important methods:
Read How to Check if a String is Bytes in Python?
Method 1: Use String Methods
Python’s built-in string methods provide a simple way to check if a string is surrounded by quotes. We’ll use the startswith and endswith methods.
def is_quoted_string(s):
return (s.startswith('"') and s.endswith('"')) or (s.startswith("'") and s.endswith("'"))
# Example
string1 = '"John Doe"'
string2 = "'Jane Smith'"
string3 = 'Michael Johnson'
print(is_quoted_string(string1))
print(is_quoted_string(string2))
print(is_quoted_string(string3)) Output:
True
True
FalseI have executed the above example code and added the screenshot below.

In this example, the is_quoted_string function checks if the input string starts and ends with either a single quote (') or a double quote ("). This method is simple and efficient for basic use cases.
Read How to Check if a String is Binary in Python?
Method 2: Use Regular Expressions
Regular expressions (regex) provide a powerful way to match patterns in strings. We’ll use the re module to create a regex pattern that checks if a string is surrounded by quotes.
import re
def is_quoted_string(s):
pattern = r'^["\'][^"\']*["\']$'
return bool(re.match(pattern, s))
# Example
string1 = '"John Doe"'
string2 = "'Jane Smith'"
string3 = 'Michael Johnson'
print(is_quoted_string(string1))
print(is_quoted_string(string2))
print(is_quoted_string(string3)) Output:
True
True
FalseI have executed the above example code and added the screenshot below.

The regex pattern r'^["\'][^"\']*["\']$' matches strings that start and end with either a single or double quote, with any characters in between. This method is more flexible and can handle more complex scenarios.
Read How to Check if a String Contains All Unique Characters in Python?
Method 3: Use Custom Functions
For more complex requirements, you can create custom functions to check if a string is surrounded by quotes. This approach allows for greater flexibility and customization.
def is_quoted_string(s):
if len(s) < 2:
return False
if (s[0] == '"' and s[-1] == '"') or (s[0] == "'" and s[-1] == "'"):
return True
return False
# Example
string1 = '"John Doe"'
string2 = "'Jane Smith'"
string3 = 'Michael Johnson'
print(is_quoted_string(string1))
print(is_quoted_string(string2))
print(is_quoted_string(string3))Output:
True
True
FalseI have executed the above example code and added the screenshot below.

This custom function checks the length of the string and verifies if the first and last characters are matching quotes. It provides a clear and understandable way to handle the task.
Read How to Check if a String Begins with a Number in Python?
Handle Edge Cases
When checking if a string is surrounded by quotes, it’s important to consider edge cases. Here are a few examples and how to handle them:
- Empty Strings: An empty string should not be considered as quoted.
- Single Character Strings: A single quote or double quote character alone should not be considered as quoted.
- Mismatched Quotes: Strings with mismatched quotes should not be considered as quoted.
def is_quoted_string(s):
if len(s) < 2:
return False
if (s[0] == '"' and s[-1] == '"') or (s[0] == "'" and s[-1] == "'"):
return True
return False
# Edge Cases
string1 = ''
string2 = '"'
string3 = "'"
string4 = '"John Doe'
string5 = 'Jane Smith"'
print(is_quoted_string(string1)) # Output: False
print(is_quoted_string(string2)) # Output: False
print(is_quoted_string(string3)) # Output: False
print(is_quoted_string(string4)) # Output: False
print(is_quoted_string(string5)) # Output: FalseRead How to Check if a String is Base64 Encoded in Python?
Conclusion
In this tutorial, I have explained how to check if a string is surrounded by quotes in Python. We covered using string methods, regular expressions, and custom functions and provided detailed examples using USA-specific names. I also discuss how to handle edge cases.
You may also like to read:
- How to Check if a String is a Boolean Value in Python?
- How to Check if a String is ASCII in Python?
- How to Check if a String is All Uppercase in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.