Following an old tradition among programmers, we're going to start our Python adventure with a "Hello, world!" program. Similar to other programming languages (e.g. R), Python comes with a print()
function that helps us achieve this task.
In [1]:
print("Hello, world!")
As seen from the above example, print()
takes a textual value (or any other Python object) as input and prints its contents (or information about the object) to a so-called output stream. In our case, this stream is directed to the console window right below the "Hello, World!" code chunk.
Alright, first step taken! Of course, print()
also comes with a set of optional arguments to provide finer control of the created output. A list of, as well as details about, these optional parameters can be retrieved from the corresponding help pages, which also provide further information about the function itself, through putting a question mark (?
) right in front of the function's name.
In [2]:
?print
The most relevant optional keyword arguments supported by print()
are (default values in brackets)
sep
: string inserted between the single input values (' '
)end
: string appended after the last value ('\n'
)file
: a file-like object aka stream (sys.stdout
)For example, suppose we have two parts of a sentence that we would like to put together to form a complete sentence but, at the same time, separate them by [...]
, which is a rather common notation in scientific literature. Taking into account the list of optional parameters, this could then be easily achieved:
In [3]:
print("This is", "some text.", sep = " [...] ")