- print() Built-in Function
- Syntax of print()
- Examples
- 1. Print string to console
- 2. Print number to console
- 3. Print variables to console
- 4. print() with a specific separator between values
- 5. Print with a specific end value
- Video Tutorial
- Summary
- Print String to Console Output in Python
- Syntax of print()
- Examples
- 1. Print a string value to console
- 2. Print multiple values to console
- 3. Print with separator
- 4. Print with different ending character
- Summary
print() Built-in Function
To print strings to console or echo some data to console output, use Python inbuilt print() function.
print() function can take different type of values as argument(s), like string, integer, float, etc., or object of a class type.
The following is a simple demonstration of how to use print() function in a Python shell.
>>> print("Hello World! Welcome to Python Examples.") Hello World! Welcome to Python Examples. >>> print(10) 10
Syntax of print()
The syntax of print() function is
print(*objects, sep=' ', end='\n', file=None, flush=False)
Examples
1. Print string to console
In this example, we will print a string to console output. Pass string as argument to the print() function.
Python Program
Yeah! That one print statement is all of the program. Run it from your command prompt or terminal using python command. Or you can run it from your Jupyter Notebook.
The string is printed to the console as shown below.
2. Print number to console
In this example, we will pass integer (number) as argument to print() function and print the number to the console.
Python Program
Any datatype passed to the print function is implicitly converted to string and printed to the console. Hence, the int we passed as argument to print() is first converted to string, and then print to the console.
3. Print variables to console
You can provide multiple values to print() function as arguments. They will be printed to console with single space as a default separator. Again, you can specify your own separator, and we shall see that in our next example.
Python Program
Please note that space is printed out to console, between x and y.
4. print() with a specific separator between values
You can pass a string for named parameter sep to override the default separator.
Python Program
x = 'pi is' y = 3.14 print(x, y, sep=' : ')
x and y have been printed to the console with the specified separator.
5. Print with a specific end value
We already know that we can specify an ending value for print() function. New line character is the default ending for print() function in Python. You can override this by passing a string to the named parameter end as shown in the below Python program.
Python Program
print('Hello', end='-') print('World', end='.\n')
Video Tutorial
Summary
In this tutorial of Python Examples, we learned how to print a message to console, a variable or any other datatype to the console, using Python print() builtin function with the help of well detailed Python programs.
Print String to Console Output in Python
Most often, we print data to console. Be it the result we want to display, or intermediate results that we want to check while debugging our program.
In this tutorial, we will learn how to print a string to console with some well detailed examples.
To print a string to console output, you can use Python built-in function – print().
Syntax of print()
The syntax of Python print() function is:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
- objects are comma separated objects that has to be printed to the file.
- sep is the separator used between objects. sep can be a string or a character. By default sep is space.
- end is the character or a string that has to appended after the objects.
- file is the place where our print function has to write the objects. By default it is sys.stdout, in other words system standard output which is console output.
If you would like to override the default values of sep, end, file or flush parameters, you can do so by giving the corresponding argument. We will see more about them in the following examples.
Examples
1. Print a string value to console
The following is a basic example, where we print the string Hello World to console.
Python Program
2. Print multiple values to console
You can also specify multiple strings separated by comma to the print function. All those strings will be considered as *objects parameter.
Python Program
In this example, we have provided two objects to the print() statement. Since the default separator is ‘ ‘ and the end character is new line, we got Hello and World printed with space between them and a new line after the objects.
3. Print with separator
Now, we will provide a separator to the print function.
Python Program
We have overridden the default separator with a character. You can also use a string for a separator.
Python Program
4. Print with different ending character
May be you do not want a new line ending. So, with print() function’s end parameter, you can specify your own end character or string when it prints to the console.
Python Program
Summary
In this tutorial of Python Examples, we learned how to print a string to console, how to specify a separator or ending to print() function, with the help of well detailed examples.