What are Strings in Python?
Strings in Python are basically sentences that include words, letters, numbers and special characters.
For example, "Hello,world !", "123", "?" etc.
print() function:
print() is an in-built function in Python. It is basically used to give print something in Python like a word, number, or just anything. For example:
print("Hello,World!")
The above command prints 'Hello, World!' when executed.
By the way if you have not created your first Python program using print() function check this article out:
Single and Double Quotes in Strings:
Notice in the above definition of Strings, the examples are inside double quotes (""). You can use single quotes ('') as well, I will tell you the difference between the two in just a bit. It is ABSOLUTELY ESSENTIAL to use quotes while using a string, because if you don't, you risk declaring it as another data type such as a float, or a boolean data type. (Don't worry I will explain what these are in later posts but for now focus on learning about Strings)
Single Quotes (' '):
Single quotes are used while declaring a string, these basically tell Python that the following characters are a part of string. We use single quotes in Python like this:
'hello', '123' etc.
Now you may ask if we use single quotes to declare a String, WHAT DO WE NEED DOUBLE QUOTES FOR??
Well just wait till I tell you the major drawback of using single quotes. It is.....
.....YOU CANNOT USE ANOTHER SINGLE QUOTE INSIDE SINGLE QUOTES. This means that the following statement is invalid in Python:
print('Tom's phone)
This statement will raise an error.
This is what it looks like in action
Ok so if you are thinking what just happened?? Why is this code not executing and giving an error??
Let me help you out, the string that you declare should be inside single quotes, this means that the string should be inside double single quotes (I know it's confusing, read it again if you didn't get it). So in the above example we tried to print 'Tom's phone'. Do you see the problem now??
In 'Tom's phone' the opening quote (i.e. the quote before 'T') got its ending quote after 'm' and before 's'. Although the whole thing was supposed to be a part of the string, Python doesn't get it. According to us, the ending quote should be after 'e' but since Python considers the second quote as the ending quote the "actual" ending quote is unpaired, thus raising the error.
So how do we tackle this problem?? SIMPLE. We use double quotes.
Double quotes (" "):
Now in the above example, we can simply use double quotes to solve the problem. We can write "Tom's phone" instead of 'Tom's phone' and it will not raise an error. It will execute the code in the following way:
AND THE PROBLEM IS SOLVED!!!
BUT WAIT some of you would go like "Hey D*****S!!! Not so quick. What if we wanna print something inside double quotes, like a statement that someone said. What then?? Will it raise an error?? Is there a way to deal with this problem??"
The answer is YES and YES. To solve this problem we are going to use a special weapon called the FORWARD SLASH !!!!
Back Slash ( \ ):
This character is used to tell Python to ignore the main meaning of the character that follows.
You can use it in the following way:
print('Tom\'s "phone"')
The output will be:
AND this is how you are going to solve this problem. (AND DON'T call me a D*****S)
As you have probably realized by now, Python loves raising error for petty things. It looks "petty" but makes Python crazy. Don't worry you will get many errors as you get more advanced but you will also learn how to avoid these errors along the way.
Printing multiple strings:
You can also print multiple strings at the same time. You can use the following methods to print multiple strings:
Method# 1:
You can use '+' operator to print multiple strings. For example:
Method# 2:
You can use the '*' operator to print multiple duplicate strings. Just use '*' in the following way:
CONGRATULATIONS!!!! You have learnt about your first data type in Python.
If you are a beginner, intermediate, advanced or just someone interested in programming, feel free to join or telegram channel and be among people like you:
And do you know the best part? Joining it is FREE !!!
So go ahead click on the link and I will see you there.
HOPE YOU HAVE AN AWESOME DAY AHEAD !!!

0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback