Basics of Python
Python print() function prints the message to the screen or any other standard output device.
Syntax: print(value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)
value(s) : Any value, and as many as you like. Will be converted to string before printed
sep=’separator’ : (Optional) Specify how to separate the objects, if there is more than one.Default :’ ‘
end=’end’: (Optional) Specify what to print at the end.Default : ‘\n’
file : (Optional) An object with a write method. Default :sys.stdout
Though it is not necessary to pass arguments in the print() function, it requires an empty parenthesis at the end that tells python to execute the function rather calling it by name. Now, let’s explore the optional arguments that can be used with the print() function.
String literals in python’s print statement are primarily used to format or design how a specific string appears when printed using the print() function.
\n : This string literal is used to add a new blank line while printing a statement.
“” : An empty quote (“”) is used to print an empty line.
print(“GeeksforGeeks \n is best for DSA Content.”)
is best for DSA Content.
end= ” ” statement
The end keyword is used to specify the content that is to be printed at the end of the execution of the print() function. By default, it is set to “\n”, which leads to the change of line after the execution of print() statement.
Example : Python print() without new line.
# This line will automatically add a new line before the
# next print statement
print (“GeeksForGeeks is the best platform for DSA content”)
# This print() function ends with “**” as set in the end argument.
print (“GeeksForGeeks is the best platform for DSA content”, end= “**”)
print(“Welcome to GFG”)
GeeksForGeeks is the best platform for DSA content
GeeksForGeeks is the best platform for DSA content**Welcome to GFG
The print() function can accept any number of positional arguments. These arguments can be separated from each other using a “,” separator. These are primarily used for formatting multiple statements in a single print() function.
b = “for”
print(“Geeks”, b , “Geeks”)
Geeks for Geeks
Contrary to popular belief, the print() function doesn’t convert the messages into text on the screen. These are done by lower-level layers of code, that can read data(message) in bytes. The print() function is an interface over these layers, that delegates the actual printing to a stream or file-like object. By default, the print() function is bound to sys.stdout through the file argument.
Example : Python print() to file
# declare a dummy file
dummy_file = io.StringIO()
# add message to the dummy file
print(‘Hello Geeks!!’, file=dummy_file)
# get the value from dummy file
Example : Using print() function in Python
# Python 3.x program showing
# how to print data on
# a screen
# One object is passed
x = 5
# Two objects are passed
print(“x =”, x)
# code for disabling the softspace feature
print(‘G’, ‘F’, ‘G’, sep=”)
# using end argument
x = 5
Running your First Code in Python
Python programs are not compiled, rather they are interpreted. Now, let us move to writing a python code and running it. Please make sure that python is installed on the system you are working on. If it is not installed, download it from here. We will be using python 2.7.
Making a Python file:
Python files are stored with the extension “.py”. Open a text editor and save a file with the name “hello.py”. Open it and write the following code:
Reading the file contents:
Linux System – Move to the directory from the terminal where the created file (hello.py) is stored by using the ‘cd’ command and then type the following in the terminal :
Windows system – Open command prompt and move to the directory where the file is stored by using the ‘cd’ command and then run the file by writing the file name as a command.
Variables in Python
Variables need not be declared first in python. They can be used directly. Variables in python are case-sensitive as most of the other programming languages.
The output is :