Help and Print

An Expected Failure

Let’s try something new that we’ve never done before and don’t yet know how to do.

Copy-paste this code into a file called grade.py:

percent = input("Grade: ")

You can run this program from your system command prompt by typing python grade.py:

$ python grade.py

Depending on your operating system you may need to type one of these commands instead:

$ python3 grade.py
> py grade.py
> py -3 grade.py

Running this program should print Grade: to the screen and then wait for you to type a grade and hit Enter. Type in a numeric grade from 0 to 100.

Now try to modify this program to print the grade out with a percent sign after it (so if the user types 85 then 85% would be printed).

For example entering 76 would then result in 76% being printed:

$ python grade.py
Grade: 76
76%

For a bonus, round the grade to the nearest whole number before printing it (so 85.7 would become 86%).

For example entering 60.6 would then result in 61% being printed:

$ python grade.py
Grade: 60.6
61%

Help

If you want to find out about more string methods, you can use the help function.

You can use help on a string object or on the str class:

>>> help(str)  

Use the space character to advance to the next page, or use your scroll controls. The letter q will exit the help display.

At the beginning of the help results you will usually see a lot of “dunder” methods; these are the ones with double underscores before and after the method name. Dunder methods are used to overload operators and customize the behavior of built-in functions.

|  __add__(self, value, /)
|      Return self+value.
|
|  __contains__(self, key, /)
|      Return key in self.
|
|  __eq__(self, value, /)
|      Return self==value.
|
|  __format__(...)
|      S.__format__(format_spec) -> str
|
|      Return a formatted version of S as described by format_spec.

Skip past the dunder methods to the methods that look like regular method names.

You can also use help to find out more about a specific method:

>>> help(str.format)  
>>> help("my string".format)  

Help can be used on other objects, too:

>>> number = 4
>>> help(number)  

If we want help on Python’s in-place addition operator, +=, we have to enter it as a string to the help function:

>>> help(+=)
Traceback (most recent call last):
  File "<stdin>", line 1
    help(+=)
             ^
SyntaxError: invalid syntax
>>> help('+=')  

Python treats strings differently than other types of objects with help. Python’s help function interprets strings as the name of a topic to look up.

If we want help on the string type we can use help(str) or help('str').

Print

So when we type an expression at the REPL, the value returned will be printed.

Once we start making Python programs, we’ll need to use the print function to print out strings.

When we use the print function it will print our strings the way non-programmer humans prefer to see them. The quotes surrounding our strings will not be shown and special characters will be converted appropriately.

To print a string over two lines we can use \n:

>>> print("Hello\nWorld!")
Hello
World!

When the REPL prints out the string, it prints out a “developer-friendly” representation, which shows the special characters in the string:

>>> greeting = "Hello\nWorld!"
>>> greeting
'Hello\nWorld!'

We can also make multi-line strings in Python by using triple-quoted strings:

>>> greeting = """Hello
... World!"""
>>> print(greeting)
Hello
World!

Note that the REPL recognizes that the line greeting = """Hello is incomplete and changes the prompt to three dots to let you know that you need to continue before Python can interpret the command. We didn’t type those three dots: Python did. Python puts ... before the line when it knows there’s more to come (in this case because we’re inside a multi-line string that hadn’t closed with """ yet).

Python allows multiplication of strings and integers to make repeated strings:

>>> print(" Hello!" * 10)
 Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello!
>>> verse = """This is the song that never ends
... It just goes on and on my friend
... Some people started singing it not knowing what it was,
... And they'll continue singing it forever just because...
...
... """
>>> song = verse * 5
>>> print(song)
This is the song that never ends
It just goes on and on my friend
Some people started singing it not knowing what it was,
And they'll continue singing it forever just because...

This is the song that never ends
It just goes on and on my friend
Some people started singing it not knowing what it was,
And they'll continue singing it forever just because...

This is the song that never ends
It just goes on and on my friend
Some people started singing it not knowing what it was,
And they'll continue singing it forever just because...

This is the song that never ends
It just goes on and on my friend
Some people started singing it not knowing what it was,
And they'll continue singing it forever just because...

This is the song that never ends
It just goes on and on my friend
Some people started singing it not knowing what it was,
And they'll continue singing it forever just because...

Note

Python 2 used a print statement instead of the print function we have in Python 3. If you find code examples online that are missing parenthesis around print, you’re looking at Python 2 code and that code likely won’t work in Python 3 without some modifications.

Help Discovery Exercises

Hint

If you get stuck for a minute or more, try searching Google or using help.

If you’re stuck for more than a few minutes, some of these links might be helpful for some of the exercises below:

Multi-line Poem

Create a variable named poem that contains at least three lines of text. The poem can be one you’ve written or a poem by someone else.

When printed, your poem variable should display multiple lines of text, like this:

>>> print(poem)
The fog comes
on little cat feet.

It sits looking
over harbor and city
on silent haunches
and then moves on.

Hint

You can create multi-line strings using triple quotes (""" or ''') or by using newline escape sequences (\n) in regular strings.

Count occurrence of word

  1. Make a multi-line string that contains the text of Frankenstein (hint: copy-paste it from the file frankenstein.txt that you downloaded to your python_class directory). Note that on Windows machines, it might not copy/paste the entire file from some editors. If you have this problem, try opening the file in the browser and copying it from there.

  2. Count the number of times the string “beautiful” (with any capitalization) appears in the text (hint: dig through the string documentation)

Format decimal places

  1. Make a variable cost and set it to 5.5

  2. Format a string that contains a dollar sign and the value of the cost variable, formatted to two contain decimal places (the result should be '$5.50'). We want the same formatting to work (make a correct string representing dollars and cents) if the value of cost is 7.232.

Use silly case

Alter the text of Frankenstein to make every word consist of a lower case character followed by all uppercase characters

Count

Count the number of words in Frankenstein.

Count the number of lines in Frankenstein.