Single Line Strings in Python Programming By Manish Sharma

Single Line Strings In Python

The concept of single and double quotes is bothering us since Python Programming tutorial 2 where I promised to do a dedicated video explaining when to use Single and Double quotation marks respectively for handling Strings in Python Programming.

Today is the day when I do that. In this article, we will learn how to use single or double quotes for handling single line strings in Python Programming.

What is String in Computer Programming?

In computer programming language, strings are nothing but a single dimension array of characters. You can define a string in easy words as “a sequence of characters”.

Example of String.

For example, Rebellion Rider is a string made up of 14 alphabetic characters and a white space which in total becomes 15 characters. Thus we can say Rebellion rider is a sequence of 15 characters which makes it eligible to be called a string.

Single line strings in Python programming by Manish Sharma

Types of Strings in Computer Programming.

In computer programming languages like Python, a string can be categorized as

  • A single Line string and
  • A multiline string

Let’s just focus on Single Line string in this tutorial and save the multiline for the next one.

How to work with Single Line String in Python Programming

In Python programming language there are multiple ways of handling a string. Among those the most common ways are:

  1. Enclose the string in Double Quotes and
  2. Enclose the string in Single Quotes

Apart from these two options, Python Interpreter provides us one more way which is for handling multi-line string. We will discuss it in another tutorial. For now, let’s learn when to use the single quote and the double quote.

How Python Interpreter Processes a String?

Before we start the demonstration, let’s first understand how Python interpreter Processes a string.

Whenever Python interpreter starts processing a string it looks for a quotation mark, it could either be a single quote or double quote. The opening quote indicates the starting of the string and closing quote indicates the end of the string. If either of these goes missing then it will raise an alarm which will end up in an error.

When To Use Single or Double Quotes?

In order to learn how and when to use Single or Double Quotes, we will take help of three different strings. These strings will be –

  1. I am learning Python from RebellionRider.com
  2. What’s Up Internet
  3. I am going to be a “Python Programmer” very soon.

What’s so special about these strings?

String 1: The first string, which is “I am learning Python from RebellionRider.com” is a very simple string. I am calling it a simple string because unlike other two strings, it does not contain any special punctuation marks such as an apostrophe or any other quotation marks. 

String 2: The Second string which is “What’s up Internet” is our favorite string. As you can see it contains an apostrophe sign in the middle. This apostrophe sign will require our special attention.

String 3: The apostrophe sign of string 2 is not the only one demanding special attention. The phrase “Python Programmer” which is enclosed in a pair of double quotes also requires some extra attention. It is not the phrase that is special but the double quotation marks in which it is enclosed that needs to be handled differently.

Example 1: Do we need a single or double quote for a simple string?

To understand whether we should use a pair of single quote or a double quote to display a simple string let’s do a demonstration. For that, we will use our first string which is “I am learning Python from RebellionRider.com”.

In the case of simple string which does not involve any special punctuation marks, the choice of using quotation mark completely depends on you. You can enclose the string either in single quotes or in double quotes. You are free to use whichever you want.

Statement 1

print (‘I am learning python from RebellionRider.com’)

Statement 2

print (“I am learning python from RebellionRider.com”)

Which means both the above shown statements are correct and will work.

Example 2: When should we use double quotes (“…”) then?

To learn when to use double quotes in Python Programming let’s take our second string which is “What’s up Internet”, our all-time favourite string. As we can see that this string contains a punctuation mark which is apostrophe in the middle (What’s). If we enclose this string into a pair of single quotes then we will get an error because the interpreter translates this apostrophe sign as a closing single quote.

In case you don’t believe me then try to print the statement given below on your system and see, what it yields.

#Wrong statement
print(‘what’s up internet’)

single line strings in python programming by manish sharma

The only legitimate string according to the Python interpreter in this print statement is ‘What’ because it has both starting as well as ending single quotes. According to the Python interpreter rest of the string is nothing but an anomaly as it is neither enclosed in a pair of double quotes nor in a pair of single quotes.

single line strings in python programing by manish sharma

Solution

If you want to print a statement which contains special punctuation signs like an apostrophe or has any phrase enclosed in single quotes in the middle then it’s best to enclose that string into a pair of double quotes. For example

print(“what’s up internet”)
#or
print(“I am learning ‘Python’ from RebellionRider.com”)

This way the interpreter will treat the opening double quote as the starting of the string and will look for the closing double quote sign for the end of the string. Any punctuation mark that comes in the middle will be bypassed.

How about the single quote then?

In order to understand when we should use single quotes with strings in Python Programming let’s take our third string which is “I am going to be a “Python Programmer” very soon.”

This is again a simple string except that it has a phrase in between which is enclosed in double quotes (“Python Programmer”).  We just need to make sure that these double quotes get translated as they intended to be.

If we enclose this string into double quotes then we will get the same error we received earlier.

print (“I am going to be a “Python Programmer” very soon”)

Try this statement and check the error.

This is because this entire string is divided into three different chunks legitimate string + anomaly + legitimate string. The first legitimate string is the only string which will get processed by the Python’s interpreter, rest will get ignored.

Solution

print (‘I am going to be a “Python Programmer” very soon’)

single line strings in python programming by manish sharma

In such scenario where you want to print a string which contains punctuation marks like double quotes (“…”) then always enclose your string in a pair of single quotes. That way the opening single quote will indicate the starting of the string and the interpreter will look for the closing single quote sign, which will indicate the end of your string. Any punctuation marks that comes in the middle will be bypassed.

That’s how and when we use single and double quotation marks for handling single line strings in Python Programming Language. Now it’s time for you to evaluate yourself. I want you to go ahead and write python programs to print these strings which are flashing on your screen.

“I talk in “Python” language!”
“I’m a ‘would be’ Python Programmer”

Let’s see how much you learnt.

That’s it for this tutorial, stay tuned as in the next tutorial we will learn how to deal with punctuation marks without switching between single and double quotes. Thanks & have a great day!