What are strings?
Strings in programming languages are used to store a contiguous set of characters. Strings are one of the most popular data types in Python and can be defined in three ways:
- using single quotes – for example:
>>> print('Hello World!') Hello World!
- using double quotes – for example:
>>> print("Hello World!") Hello World!
- using triple double or single quotes – the triple quotes enable you to define strings spanning multiple lines. For example:
>>> print('''Hello world! This is a new line.''') Hello world! This is a new line. >>>
The reason why both the single and double quotes are used is so you can embed a quote character of the other type inside a string. For example, you may embed a single-quote character in a string enclosed in double-quote characters:
>>> newString = "Mark's car" >>> print(newString) Mark's car