100 days of code: Day 3 - variables, strings, methods

Last modified on 2020-06-11

Source: 100-days-of-code.

Variables are labels assigned to values. Strings are a data type composed of a series of characters between single or double quotes. String methods can perform operations on a string such as change to upper or lower case.

So …

name = 'carl sagan'
print(name)
print(name.upper())
print(name.title())

… generates …

carl sagan
CARL SAGAN
Carl Sagan

Using f-strings (f stands for format) enables the insertion of a variable’s value into a string …

name = 'carl sagan'
message = f'The scientist, astronomer, and author, {name.upper()} ...'
print(message)

… generates …

The scientist, astronomer, and author, CARL SAGAN ...

Name of the variable enclosed in braces is replaced with its value.

Bring it all together in printing a quote from Carl Sagan by creating quote.py

name = 'carl sagan'
source = 'source: cosmos, part 11: the persistence of memory'
quote = (f"The scientist, astronomer, and author, {name.upper()}, "
        + "on the magic of books:\n\n"
        + "\"What an astonishing thing a book is. It's a flat object made \n"
        + "from a tree with flexible parts on which are imprinted lots of\n"
        + "funny dark squiggles. But one glance at it and you're inside the\n"
        + "mind of another person, maybe somebody dead for thousands of\n"
        + "years. Across the millennia, an author is speaking clearly and\n"
        + "silently inside your head, directly to you. Writing is perhaps\n"
        + "the greatest of human inventions, binding together people who\n"
        + "never knew each other, citizens of distant epochs. Books break\n"
        + "the shackles of time. A book is proof that humans are capable\n"
        + "of working magic.\"\n\n"
        + f'{source.title()}')

print(quote)

Happy hacking!