Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Use Double Quotation in Python



Double Quotes in Python

Double quotes are used to represent a string or a character. The functionality of the single quotes and double quotes is the same. We can use any type of quote as per our requirement.

The following is the syntax to create a string using double quotes in the Python programming language.

str = "text"

Example

Let's see an example to understand the functionality and usage of the double quotes in Python. Initially, we have created a string by passing the sentence in double quotes and assigned it to the variable str.

Next we printed the created string by calling the string variable name st. After that, we printed the type of the string by using the type function.

str = "Double quotes in python programming"
print(str)
print(type(str))

Output

Following is an output of the above code -

Double quotes in python programming
<class 'str'>

Combination of Single and Double Quotes

In Python, single and double quotes have the same priority. We can use single quotes inside double quotes and vice versa. Using the same type of quote inside a string (e.g., a single quote inside single-quoted text), we need to use an escape character (\) before it.

For example, to handle contractions like 's (as in 'It's raining'), we must escape the single quote.

Example

In the following example, we have used double quotes inside the single quotes, and to handle the 's escape character is used -

my_str = 'It's "sunny"  day'
print(my_str)

Following is an output of the above code -

It's "sunny"  day

Handling Escape characters within Quotes

We use escape characters within quotes, they affect the string by performing special functions. To prevent this behavior and treat backslashes as literal characters, we use a raw string by prefixing the string with r.

Example

In the following example, the string contains \n, which is an escape character to prevent its functionality, a backslash is used -

my_str="c:\doc\navya"
print("Without raw string-")
print(my_str)
print("Using raw string -")
print(r'c:\doc\navya')

Following is an output of the above code -

Without raw string-
c:\doc
avya
Using raw string -
c:\doc\navya
Updated on: 2025-05-29T11:27:14+05:30

588 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements