string = "this is string" #or string = 'this is also string'
Here is what the above code is Doing:
1. We are creating a variable called string and assigning it a value of “this is string”.
2. We are creating a variable called string and assigning it a value of ‘this is also string’.
The difference between the two is that in the first case, we are using double quotes and in the second case, we are using single quotes.
The reason we have two different ways of representing strings is that sometimes we need to use one type of quote inside of another. For example, let’s say we want to create a string that contains the following sentence:
I’m going to the store.
If we use double quotes, we would have to write the following:
string = “I’m going to the store.”
However, if we use single quotes, we can write the following:
string = ‘I\’m going to the store.’
The backslash is called an escape character. It is used to escape characters that have special meaning in Python. In this case, the single quote has special meaning in Python (we’ll learn what this means later), so we need to escape it.
There are other escape characters in Python. For example, if we want to create a string that contains a newline, we can use the following:
string = “this is the first line\nthis is the second line”
The above code would create a string with the following value:
this is the first line
this is the second line
There are many other escape characters in Python. You can find a list of them here: