In this tutorial, we will look at how to replace single quotes in a Python string with double quotes with the help of some examples.
Replace '
with "
inside a string
Let’s see an example to better understand the task at hand. Let’s say you have the string, "This is a 'safe' place"
. After replacing the single quotes with double quotes, the resulting string should be 'This is a "safe" place'
.
How to replace single quotes with double quotes in a string?
You can use the following methods to replace single quotes with double quotes in a Python string.
- Using the string
replace()
function to replace all single quotes with double quotes. - Using a translation table to map every single quote with a double quote.
Let’s now look at both these methods with the help of some examples.
Example 1 – Using the string replace()
function
The built-in string replace()
function used to replace occurrences of a string with another string. To replace single quotes with double quotes, pass a single quote as the first argument and a double quote (using escape sequence) as the second argument.
Let’s look at an example.
# create a string s = "This is a 'safe' place" # display the string print(s) # replace single quotes with double quotes res = s.replace("'", """) # display the result print(res)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
This is a 'safe' place This is a "safe" place
You can see that the single quotes have been replaced by double quotes in the resulting string.
Example 2 – Using the string translate()
function
The string translate()
function is used to apply a translation map on a string. Use the str.maketrans()
function to create a translation map (also called translation table), mapping quotes to double quotes.
Let’s look at an example.
# create a string s = "This is a 'safe' place" # display the string print(s) # replace single quotes with double quotes res = s.translate(str.maketrans({"'": """})) # display the result print(res)
Output:
This is a 'safe' place This is a "safe" place
The single quotes have been replaced by the double quotes in the resulting string.
You might also be interested in –
- Swap the Case of a Python String with the swapcase() method
- Swap Adjacent Characters in a Python String with this Method
- Python String Replace – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.