A string is a collection of characters, which can include letters, numbers and even punctuation.
A string can be a data type and can be assigned to a variable name. Whenever we give a variable name string data, we put place the string inside quotations marks. Take a look at this example:
string_variable = "I am a string!"You can also use single quotations to define a string, it doesn't matter:
another_string = 'I am also a valid string!'Sometimes your string may need to include an apostrophe or quotation marks, and so you need to tell Python when to ignore such punctuation. The following code will produce an error, because Python will attempt to end the string prematurely:
a_string = "Mark is a boy and he says "hi!", Mark also has a cat."The string in this code will end at the first quotation mark within the string, just before hi!. This is because Python detects that this is a string, thanks to the very first quotation mark, and it looks for the end of the string in the form of another quotation mark. From hi!" ... to the very end of the string is not understood by Python and so an error will occur.
It is perfectly OK to use a single apostrophe within a string that has been outlines with double quotation marks, as in this example:
a_string = "Mark is a boy and he says 'hi!', Mark also has a cat."And you can also swap these around just like this:
a_string = 'Mark is a boy and he says "hi!", Mark also has a cat.'I want to write a string that looks like this:
string = '"I am writing a Python string", Said Harry's mum. 'Nice!' Thought Harry.'Running this code will output the following in Python 3:
SyntaxError: invalid syntaxAnd the string will attempt to end at the end of the first instance of the word Harry, where an apostrophe appears as it refers to Harry's mother.
Therefore the string looks like this:
string = '"I am writing a Python string", Said Harry'And the rest of the text from s.. on wards, will cause Python to run an error because it doesn't know what the rest of the text is meant to be!
There is a way to make Python ignore a piece of punctuation's original meaning and to include it in the string it is written within, and that invovles using the '' mark, or the backslash, directly before the punctutation mark you want python to ignore.
So, our string will eventually look like this:
string = '"I am writing a Python string", Said Harry\'s mum. \'Nice!\' Thought Harry.'
print (string)
>>> "I am writing a Python string", Said Harry's mum. 'Nice!' Thought Harry.Ensure you place your backslash directly in front of the punctuation mark you want Python to ignore, with no spaces between them. As you type strings like this into your text editor, placing a \ will probably cause the text editor to highlight the backslash and proceeding punctuation mark in a different color.