String Literals in Python

5205

This article explains what is string literal and what are the different ways to assign strings literal to a string variable using single and double-quotes.

What is a String Literal

Literal refers to a specific value or a fixed value or a constant that is used in the program. Normally when literal consists of a single character, it refers to character literal. When literal consists of more than one character, it refers to as String literal. But, in the case of Python language, there is no character data type, so all single characters are strings with length one.

Code: 
a_string_variable = 'Python Programming Course'

 

In the above code, the ‘Python Programming Course’ is a string literal and a_string_variable is a string variable. In short, the string variable points to a string literal. Different string variables can point to a single literal but one string variable can point to only one literal at a given time.

How to represent string literal in Python

String literal can be placed inside single quotes or double-quotes. The more formal way is to say that String literal is represented using single or double-quote delimiters.

Assigning string literal to a variable using single quotes as delimiters.

Code: 
single_quote_variable = 'Hi I am placed inside single quotes'

 

Assigning string literal to a variable using double quotes as delimiters.

Code: 
double_quote_variable = "Hi I am placed inside double quotes"

Combination of Single quote and Double quotes to represent String Literal

In python programming, there is no difference between string literal when placed inside single or double-quotes. It means there is no semantic difference between these two representations, it differs only in syntax.

Code: 
single_quote_variable = 'Hi I am placed inside single quotes'

double_quote_variable = "Hi I am placed inside double quotes"


 

There may be a scenario when you want to use a single quote or double quote as a character in the string literal. Python is so cool regarding that.

You can simply place a double quote inside the string literal when using single quotes as delimiters. Similarly, you can place a single quote inside the string literal when using single quotes as delimiters. If you are confused, go through the below python code and output.

 

Code:
print('Place " double quotes when using single quote as delimiter')
print("Place ' single quotes when using double quote as delimiter")
a_double_inside_single = 'Place "Watch me" double quotes when using single quote as delimiter'
b_single_inside_double = "Place 'Watch me' single quotes when using double quote as delimiter"
print(a_double_inside_single)
print(b_single_inside_double)

 

Output:
Place " double quotes when using single quote as delimiter
Place ' single quotes when using double quote as delimiter
Place "Watch me" double quotes when using single quote as delimiter
Place 'Watch me' single quotes when using double quote as delimiter

 

Backslash character has special interpretation when placed inside Single quote and Double quotes. 

Before explaining the importance of Backslash character. Think of a scenario when you need to place a single quote in string literal delimited by single quotes. Similarly, how will you place a double quote in string literal delimited by double quotes?

Code:
print('Place ' single quote when using single quote as delimiter')
print("Place " double quotes when using double quote as delimiter")

 

Output:
    print('Place ' single quote when using single quote as delimiter')
                        ^
SyntaxError: invalid syntax
Explanation:

As soon as you place, the single quote character inside the string literal delimited by single quotes, the placed single quote character will act as the delimiter and the remaining string will not be delimited.

So, here comes the backslash character to the rescue. When a single quote is preceded by a backslash, it instructs the Python Interpreter to ignore the special meaning of a single quote and consider it as an ordinary character. The same goes for the double-quotes.

Code:
print('Place \' single quote with backslash when using single quote as delimiter')
print("Place \" double quotes with backslash when using double quote as delimiter")

 

Output:
Place ' single quote with backslash when using single quote as delimiter
Place " double quotes with backslash when using double quote as delimiter

Wait, it does not end here. Backslash has another purpose too. As we see, the backslash hides the special meaning of a single quote and double quote. Similarly, some characters when preceded by a backslash have special meaning. In the below code we are using \n i.e when n is preceded by a backslash it refers to new-line.

Code:
print("I am a message and \nI will print in two separate lines")
print(r"I am a message and \nI will print in two separate lines")

 

Output:
I am a message and
I will print in two separate lines
I am a message and \nI will print in two separate lines
The first print function will print in two separate lines because of the new-line character.  But in the second case, r hides the special meaning of character preceded with a backslash.

 

You can use triple quotes to enclose strings with more than one line

Triple quotes can be a combination of either single quotes or double quotes

Code:
print('''I am a string literal
... has more than one
... line
....placed inside triple single quotes ''')
print("""I am a string literal
... has more than one
... line
....placed inside triple double quotes """)

 

Output:
I am a string literal
... has more than one
... line
....placed inside triple single quotes
I am a string literal
... has more than one
... line
....placed inside triple double quotes