Table of Contents


Conceptual Definition#

Though we usually turn to computers to work with numbers, we can also use them to work with alphabets, symbols, and/or numbers- in other words, a sequence of various characters that are are formally called strings.

Basics of strings#

Python, like most programming languages, can manipulate strings. Strings can be enclosed in single quotes ('...') or double quotes ("...") with the same result. For example:

'spam eggs'  # Single quotes.
'spam eggs'

If you need to use a quote within the string itself, (e.g., to write words involving apostrophes), then do one of the following:

'doesn\'t'  # Use \' to escape the single quote...


"doesn't"  # ...or use double quotes instead.

This use of backslash (\) allows one to escape quotes for all special characters. This is sufficient for us to work with the Vectors Tutorial.

Basics of strings (contd.)#

In the Jupyter notebooks, the output string is enclosed in quotes. The string is enclosed in double quotes if the string contains a single quote and no double quotes, otherwise it’s enclosed in single quotes. The print() function produces a more readable output by omitting the enclosing quotes and by printing escaped and special characters:

'"Isn\'t," she said.'
'"Isn\'t," she said.'
print('"Isn\'t," she said.')
"Isn't," she said.
s = 'First line.\nSecond line.'  # \n means newline.
s  # Without print(), \n is included in the output.
'First line.\nSecond line.'
print(s)  # With print(), \n produces a new line.
First line.
Second line.

If you don’t want escaped characters (prefaced by \) to be interpreted as special characters, use raw strings by adding an r before the first quote:

print('C:\some\name')  # Here \n means newline!
print(r'C:\some\name')  # Note the r before the quote.