A closer look at strings, and some common string methods.
We will commonly be working with text data, or 'strings' in Python (and other languages). Strings are expressed using single ( ' ), double ( " ), or triple ( ''' ) quotes (a triple quote is a single or double quote three times).
>>> 'This is a string of characters' # Single quotes 'This is a string of characters' >>> "This is also a string of characters" # Double quotes "This is also a string of characters"
Strings can be referred to by variables, for later usage.
>>> a_string = "I really enjoy working with strings." >>>
Notice that when we assign the memory location of the string to a variable called 'a_string' the interpreter simply displays a new prompt. This differs from when we typed a string directly at the prompt, without a variable assignment.
We can easily retrieve the string by referencing the 'a_string' variable.
>>> a_string 'I really enjoy working with strings.'
Note that the variable 'a_string' does not actually contain the string. Rather, the string is stored in the computer's memory, and the variable stores the memory address where the string is stored.
Triple quotes are used when you have a string that spans multiple lines, such as a haiku.
>>> haiku = '''Steadfastly study Python with many friends here at Peer to Peer U''' >>> print(haiku) Steadfastly study Python with many friends here at Peer to Peer U
These triple quoted strings are used less frequently than single (and double) quoted strings.