Print Quotes in Python

Print Quotes in Python

In Python and every other programming language, we can place '' (single quotation marks) inside "" (double quotation marks) and "" inside '' but we can not place '' inside '' and "" inside "". The reason behind the same is how compilers and interpreters interpret strings. They are expected to start with a " or ' and end with a " or ' respectively.

But there are a few ways to place whatever quotation marks we want inside whatever quotation marks we use to represent strings. This article will introduce those ways for Python programming language.

We can use quotation marks inside quotation marks in Python using the escape operator or \ character. The escape operator is used to escape or ignore a character inside a string. Hence, we can use it for our use case. Refer to the following Python code for the same.

a = "Double Quotation Marks: \"\""
b = 'Single Quotation Marks: \'\''
print(a)
print(b)

Output:

Double Quotation Marks: ""
Single Quotation Marks: ''

As we can see, a represents the string with the help of "" and uses "" inside it, and b uses '' to represent the string and also inside it. The \ character guides the Python interpreter to ignore the character immediately next to it. So, the Python interpreter ignores ' and ", and prints them like regular strings.

Apart from using the escape operator, we can also use spanning strings to use quotation marks inside quotation marks. Spanning strings are generally used for adding multi-line comments, but they are actually not comments. Comments are represented by a hashtag or #. Spanning strings have the str datatype.

Spanning strings start with ''' (tripe single quotation marks) or """ (tripe double quotation marks) and end with ''' or """ respectively. To understand how to quotation marks inside quotation marks with the help of spanning strings, refer to the following Python code.

a = """
Double Quotation Marks: ""
Single Quotation Marks: ''
"""
print(a)

Output:


Double Quotation Marks: ""
Single Quotation Marks: ''

Inside spanning strings, we do not have to use an escape character to ignore single and double quotation marks. In case we have to use ''' or """ inside spanning strings, we have to use an escape character. The following Python code depicts how to.

a = """
Triple Double Quotation Marks: \"""
Triple Single Quotation Marks: '''
"""
b = '''
Triple Double Quotation Marks: """
Triple Single Quotation Marks: \'''
'''
print(a)
print(b)

Output:


Triple Double Quotation Marks: """
Triple Single Quotation Marks: '''


Triple Double Quotation Marks: """
Triple Single Quotation Marks: '''
Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

LinkedIn GitHub

Related Article - Python Print

  • Print Multiple Arguments in Python
  • Print With Column Alignment in Python
  • Print Subscripts to the Console Window in Python
  • Fix Missing Parentheses in Print Error in Python
  • Print % Sign in Python