Strings Flashcards

1
Q

Strings are used when you need to process text (like names of all kinds, addresses, novels, etc.),

A

not numbers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Let’s assume that we want to print a very simple message saying:

I like “Monty Python”

How do we do it without generating an error? There are two possible solutions.

A

escape character, which you should remember is played by the backslash. The backslash can escape quotes too. A quote preceded by a backslash changes its meaning - it’s not a delimiter, but just a quote. This will work as intended:

print(“I like "Monty Python"”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

second solution may be a bit surprising. Python can use an apostrophe instead of a quote. Either of these characters may delimit strings, but you must be consistent.

A

If you open a string with a quote, you have to close it with a quote.

If you start a string with an apostrophe, you have to end it with an apostrophe.

This example will work too:

print(‘I like “Monty Python”’)

Note: you don’t need to do any escaping here.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

We’ve shown it already, but we want to emphasize this phenomenon once more - a string can be empty - it may contain no characters at all.

A

An empty string still remains a string:

’’
“”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly