There are lots of little details to understand when we work with strings in
Python, and that is what we'll cover here in this demo.
A string is a sequence of characters specified within double quotes or
single quotes.
Here is a single quoted string,
the very same string can be specified within double quotes as well.
And that is also a string and
behaves in exactly the same way as a single quoted string.
You can use three different kind of quotes to create strings in Python.
So you can have a double quotes,
you can see the word python is enclosed in double quotes.
You can use single quotes, the word is, is enclosed in single quotes, or you
can specify a string using three double quotes on either side of the string.
You can see the specification for
the word awesome, which is also a string in Python.
And you can concatenate all of these strings together to get python is
awesome.
We've already seen that a string can be multiplied by an integer.
And the result will be a string with the original string repeated that
many number of times.
Here the word python has been repeated three times
thanks to our multiplication by three.
Other arithmetic operations don't make sense in the context of strings,
such as subtraction.
You can't really subtract an integer from a string, and
Python will accordingly throw an error.
Python has this special function called the input function.
Which allows Python to accept input from the user, and
this input is in the form of a string.
The input function that you see here invoked on screen
will accept user input and assign that user input to the answer variable.
You will then use the string in the answer variable and
print some information out to screen, You had answer, That sounds delicious!
When you hit Shift+Enter and execute this bit of code, you'll find that
an input text box comes up on screen within your Jupyter Notebook.
Waiting for your input.
You can type in something meaningful into this text box, such as pasta.
The string pasta will be assigned to the answer variable, and
you get this result printed to screen.
You had pasta, That sounds delicious!
This is from our print statement.
Now why does it matter that Python accepts strings using both single as well
as double quotes?
Let's see an example where the quotes actually matter.
Let's say that your string itself included a single quote,
such as the word I'm, here in this example.
After the I we have a quote.
And Python, because we've enclosed the entire string in single quotes,
assumes that the second quote after I closes the string.
So when you execute this bit of code,
Python will tell you the syntax is invalid.
Because it doesn't understand what the rest of the characters after the I
represent.
If your string itself includes a single quote,
you should enclose that string using double quotes.
Such as the example you see here on screen.
I'm learning python is enclosed within double quotes, and Python correctly
interprets the single quote within the string as a part of the string.
Similarly, if your string itself includes double quotes,
you should enclose the string as a whole in single quotes.
John said, Hello there!
Hello there has double quotes around it, that is a part of our string,
which is why the entire string has been enclosed in single quotes.
And when you execute this code, you'll see that Python correctly interprets
Hello there within double quotes as a part of the string.
If your string itself includes a single quote and
you have to enclose your string in single quotes for some reason.
You need to escape the single quote within your string.
Notice the backslash character just before the single quote before m.
This backslash is termed as an escape character.
And it tells Python whatever character comes after this backslash,
accept that character as is, as a part of the string.
Don't apply any special meaning to that character.
So the single quote just before m will not be treated as
the termination of a string, it'll just be treated as a part of the string.
Escape characters work well, but it makes code hard to read,
which is why they're not often preferred.
Here is an example where we use double quotes within a string which is
enclosed in double quotes.
So the quotes within the string have been escaped
using the backlash character.
Thanks to the backslash, which escapes the quotes within the string,
the quotes are interpreted correctly by our Python interpreter.
Here are some more slightly more complex examples using quotes and strings.
So the entire string is enclosed in single quotes, which means we can use
double quotes within the string without escaping the double quotes.
Any single quote within the string has to be escaped as we've done.
If you want to include a new line within a string,
you can do this using the \n special character.
\n will tell Python to add a new line between Hello John! and How are you?
And this is what you see here in the result.
You can have multiple new lines within a string if you specify
multiple \n's in your string.
Here is a very long string that spans
three lines thanks to the multiple \n's we have in there.
Using special characters within string often reduces the readability of
our code.
Which is why if you have multi-line strings, you should use the triple quote.
Here is a single line string enclosed within triple quotes.
Now, this is no different than a string specified within single quotes or
double quotes.
Let's say you have a string that includes a new line.
You want the string to span multiple lines.
Instead of using the \n,
you could simply specify the string using three single quotes.
You can see that the resultant string spans two lines,
there is a new line right after the word long.
So for strings that span multiple lines, use the triple quotes.
You can use three single quotes or three double quotes,
they work exactly the same way.
So rather than using the new line character, the best practice in Python
is to use the triple quote for multi-line strings.
You can see a number of different examples here.
All of these are strings with multiple new line characters.
All of these work, and they can be specified using triple quotes.
The triple quotes here are a must.
Try specifying a multi-line string,
which has a new line character within it using double quotes.
And you'll see that Python will immediately throw an error.
It's not able to parse the new line within double quotes.