Index
Introduction
The print()
function prints the specified message to the screen, or other standard output device.
The message can be a string, or any other object, the object will be converted into a string before written to the screen.
Syntax
print(*args, sep=' ', end='\n', file=None, flush=False)
Brackets use: if you want to print a string directly from the print function you can open a string with:
- ” : doing so you can use ’ without closing the string
- ’ : doing so you can use ” without closing the string
*args use: you can print all the element in a iterable object
All the different ways of printing a string
f-string
f-strings allow you to easily incorporate Python expressions into strings using the syntax {expression}
.
Here are some examples of f-strings in Python:
oss: Using f-strings let you print print data types different form a sting without converting them
Print parameters
The print function has some parameters that let you interact with the output of the function
End Parameters (optional)
The end parameter decide witch character goes at the end of the print
By default Python‘s print() function ends with a newline
But it can be changed with every string or Python Escaping Sequences
Sep Parameters (optional)
The step parameter decide witch character separates 2 o more objects, by default its a space symbol (” ”)
but it can be change with every string or Python Escaping Sequences
File (optional)
In Python, you can print objects to the file by specifying the file parameter.
By default is set as sys.stdout
which prints objects on the screen
But it can be change with every file thats open in writing mode
file content:
--------------------------
1
2
3
--------------------------
This program in particular tries to open python.txt, but if it doesn’t exist in the directory that you are working in it will be created
oss: every time you open the file in this way all the text inside the file will be cleared
Read Python Files to learn more
Flush parameter (optional)
- Boolean value, specifying if the output is flushed (True) or buffered (False).
- ByDefault is False|