Python Tutorial 05: How to define a string in Python?

This blog illustrated how to define a string in Python?

In Python, we can either use a single quote (‘) or double quote (“) to quote  a string.

IE. This is a string<– Double Quote


This is a string too!‘ <– Single Quote

When we want to define a string where a single quote is actually part of a string we can use the double quote (“) character to define the string.

For example:

This is Peters book.

We are using a double quote to quote the string that contains a single quote.

This is what we called Awesome!

However, what if we want to define a string that have both single quote and double quote in the string?

In that case, we will place a “\” as a special character in front of the quote. The “\” character is to advise Python that the next character is representing it’s original meaning.

For Example:

car = “He’s the best pivot. We called him \”Superman\”.”

The “\” before the double quote instructed Python to treat the next double quote character (“) as a normal double quote character only.

String definition in Python

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.