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.
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:
- Enclose the string in Double Quotes and
- 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 ā
- I am learning Python from RebellionRider.com
- Whatās Up Internet
- 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ā)
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.
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ā)
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!