In Python, you can create strings using either single quotes (”) or double quotes (“”). In this tutorial, we will learn how to create a string with double quotes in Python.
To create strings with double quotes in Python, we can use various methods like the below:
- Using Escape Character
- Using Triple Quotes
- Using String Concatenation
- Using the ‘format’ Method
- Using f-strings
Create a String with Double Quotes (“”) in Python
Now, let us see how we can use various methods to create strings in Python using double quotes.
1- Simple String using double quotes.
Here is a very basic example in Python to create a string using double quotes.
mystring = "United States of America"
print(mystring)
You can see the output.
2- Using Escape Character
To include double quotes inside a string in Python, you can use the escape character, which is a backslash (). By placing a backslash before a double quote, Python will understand that it should be part of the string and not the end of it.
Example:
city_name = "New York, also known as \"The Big Apple\""
print(city_name)
Output:
New York, also known as "The Big Apple"
3- Using Triple Quotes
You can also use triple quotes to define a string with double quotes inside in Python. You can use either triple single quotes (”’) or triple double quotes (“””).
Example:
city_name = '''Los Angeles, the "City of Angels"'''
print(city_name)
Output:
Los Angeles, the "City of Angels"
4- Using String Concatenation
You can create a string with double quotes by concatenating parts of the string that are enclosed in single quotes in Python.
Example:
city_name = 'Chicago, the "Windy City"'
print(city_name)
Output:
Chicago, the "Windy City"
5- Using format() method
You can use the ‘format’ method to insert double quotes into a string.
Example:
city_name_template = 'San Francisco, the "{}"'
nickname = "Golden Gate City"
city_name = city_name_template.format(nickname)
print(city_name)
Output:
San Francisco, the "Golden Gate City"
6- Using f-strings
F-strings, also known as “formatted string literals”, were introduced in Python 3.6. You can use f-strings to create a string with double quotes in Python.
Example:
nickname = "City of Brotherly Love"
city_name = f'Philadelphia, the "{nickname}"'
print(city_name)
Output:
Philadelphia, the "City of Brotherly Love"
Conclusion
In this tutorial, we covered various methods to create a string with double quotes in Python. By using escape characters, triple quotes, string concatenation, the ‘format’ method, or f-strings, you can easily create a string with double quotes in Python.
You may also like the following Python string tutorials:
- How to Create String with Single Quotes in Python
- How to Create a String with Newline in Python
- Split a String Using Regex in Python
- How to split a string into equal half in Python?
- Print quotes in Python [6 methods]
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.