In this lesson, we will discuss what a string is and what role quotation marks play in code. You will explore the special symbols and learn how to use them.
The definition of a string is quite simple. It is a set of characters. Imagine we have these entries:
'Hello'
'Goodbye'
'G'
' '
''
All of them are strings:
'Hello'
and'Goodbye'
are obvious, we have already worked with similar constructions and called them strings'G'
and' '
are also strings, but they only have one character each''
is an empty string, so it has zero characters
Quotation marks
We consider anything inside quotation marks a string, even if it is just a space, a single character, or no characters at all.
In the example above, we wrote the strings in single quotes, but this is not the only way. You can also use double quotes:
print("Dracarys!")
Now imagine you want to type the line Dragon's mother
. The apostrophe before the letter s
is the same symbol as the single quotation mark. Let us print it:
print('Dragon's mother')
# SyntaxError: invalid syntax
This program will not work. Python reads that the line started with a single quote and ended after the word dragon
. Next, it sees the s mother
characters without quotation marks, so it is not a string. And then there was a one-line-opening quotation mark that was never closed: ')
. This code contains a syntax error – you can notice it by the code highlight.
To avoid this error, we use double quotes. This version of the program will work correctly:
print("Dragon's mother")
Now the interpreter knows that the string started with a double quotation mark and must end with a double quotation mark. And the single quote inside has become part of the string.
It works the other way too. If you want to use double quotes inside a string, you should enclose the string in single quotes. And the number of quotation marks inside does not matter.
Now imagine we want to create this string:
Dragon's mother said "No"
It has both single and double quotes. We need to tell the interpreter that the quotation marks are one of the characters inside the string, not the beginning or the end.
We can use the escape character for this — a backslash \
. If we put \
in front of a single or double quotation mark, the interpreter will recognize the quotation mark as an ordinary character inside the string, not the beginning or the end:
# We escape the quotation marks around `No`
# The interpreter recognizes it as part of the string
print("Dragon's mother said \"No\"")
# => Dragon's mother said "No"
In the example above, we did not have to escape the single quote 's
because we created the string with double quotes. If we surrounded it with single quotes, the escape character would be used before the apostrophe, not before the double quotes.
This rule still works if you put a backlash in the string. Like any other special character, we must escape it:
print("\\")
# => \
Escape sequences
Here is some dialog we want to show:
- Are you hungry?
- Aaaarrrgh!
Let us try to display a string with this text:
print("- Are you hungry?- Aaaarrrgh!")
# => - Are you hungry?- Aaaarrrgh!
As you can see, the result was not what we wanted. The interpreter arranged the lines one after the other, not one below the other. We should tell the interpreter to press Enter — to start a new line after the question mark. We can do it with the \n
symbol:
print("- Are you hungry?\n- Aaaarrrgh!")
# => - Are you hungry?
# => - Aaaarrrgh!
The \n
symbol is an escape sequence. These sequences are also called control constructions. The interpreter does not display them in the same way as how we type them.
When we type text in Word, we press Enter at the end of a line. The editor puts an invisible character at the end of the line called LINE FEED (LF).
In some editors, you can display the invisible characters. Then the text will look something like this:
- Hey!¶
- Oh, hey! ¶
- How is it going?
The device that outputs the corresponding text takes this character into account. For example, the printer drags the paper up to one line when it encounters the LF, and the text editor drags all subsequent text below by one line.
There are several dozen such invisible characters, but there are usually only a few in programming. In addition to the line feed, there can also be:
- Tab
\t
— the gap that you get when you press Tab - Carriage return
\r
— only works in Windows
You can recognize these control constructions in the text by the \
symbol. Programmers often use line feed \n
to properly format text. For example, if we write this code:
print("Gregor Clegane\nDunsen\nPolliver\nChiswyck")
You will see on the screen:
Gregor Clegane
Dunsen
Polliver
Chiswyck
When working with the line feed symbol, consider the following points:
It does not matter what comes before or after
\n
— a character or an empty string. The line feed will be detected and executed in any caseThe string can only contain
\n
:print('Gregor Clegane') # A text string print("\n") # A string with invisible line feed characters print('Dunsen') # A string with the text
The program will display:
Gregor Clegane Dunsen
In the code, the sequence
\n
looks like two characters, but from the interpreter's point of view, it is one characterIf you want to output
\n
as text (two separate printable characters), you can use escaping - add another\
at the beginning. The sequence\\n
will appear as characters\
andn
that follow each other:print("Joffrey loves using \\n") # => Joffrey loves using \n
Windows uses \r\n
to translate strings by default. This combination works well on Windows but creates problems when transferring to other systems. For example, when there are Linux users in the development team.
The point is that the sequence \r\n
has a different interpretation depending on the chosen encoding, which we will discuss later. For this reason, it is common among developers to always use \n
without \r
.
In this case, the line feed is always treated the same and works fine in any system. Remember to configure your editor to use\n
.
Concatenation
In web development, programs use strings all the time. Everything we see on websites, in one way or another, is presented as text. This text is usually dynamic, meaning it comes from different parts joined together.
To join strings, you need to perform concatenation:
# The operator is the same as for adding numbers
# But it has a different meaning here
print('Dragon' + 'stone') # => Dragonstone
We always concatenate strings in the order in which we write the operands. The left operand becomes the left part of the string, and the right one becomes the right part. Here are a few more examples:
print('Kings' + 'wood') # => Kingswood
print('Kings' + 'road') # => Kingsroad
print("King's" + 'Landing') # => King'sLanding
As you can see, we can concatenate strings even if they have different quotes.
Spaces are a character like any other, so how many spaces you put in a string is how many you get in the final one:
# Put a space in the left part
print("King's " + 'Landing') # => King's Landing
# Put a space in the right part
print("King's" + ' Landing') # => King's Landing