Skip to main content

5.3) Strings


A string (str) is another type of data in Python. It is used to store a piece of text such as "word". It is a sequence of characters one after the other. In Python, strings are indicated by single, double quotation marks:

>>> my_string="Hello, World!"
>>> my_string
'Hello, World!'

Double quotation marks can contain single quotation marks in the string and single quotation marks can contain a double quotation mark. Triple quotation marks can contain both types of quotation marks.

>>> "This string's got a single quotation mark"
>>> 'He said "Here is a double quotation mark" and left'
Triple quotation marks can also be on multiple lines.
>>> ''' This string
Spans multiple
Lines'''

As we’ve mentioned already, triple quotation marks are often used on their own at the start of a program as documentation.

You can get the length of a string with:

>>>len(my_String)
17

Note that in Python a string of length 1 is still a string; unlike some other languages, Python doesn’t have a 'character' type.