0% found this document useful (0 votes)
25 views8 pages

Python Basics2

Python Print() Statement: How to Print with Examples

Uploaded by

Sridhar M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Topics covered

  • software development,
  • print command,
  • Python output,
  • print function applications,
  • print multiple items,
  • coding best practices,
  • Python print usage,
  • programming languages,
  • print function characteristics,
  • Python functions
0% found this document useful (0 votes)
25 views8 pages

Python Basics2

Python Print() Statement: How to Print with Examples

Uploaded by

Sridhar M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Topics covered

  • software development,
  • print command,
  • Python output,
  • print function applications,
  • print multiple items,
  • coding best practices,
  • Python print usage,
  • programming languages,
  • print function characteristics,
  • Python functions

12/13/24, 11:28 PM Python Print() Statement: How to Print with Examples

Python Print() Statement: How to Print with


Examples
By : Logan Young Updated August 12, 2024

Python print() function


The print() function in Python is used to print a specified message on the screen.
The print command in Python prints strings or objects which are converted to a
string while printing on a screen.

Syntax:

print(object(s))

Table of Contents:

How to Print a simple String in Python?


More often then not you require to Print strings in your coding construct.

Here is how to print statement in Python 3:

Example: 1
To print the Welcome to Guru99, use the Python print statement as follows:

This code is editable. Click Run to Execute

1 print ("Welcome to Guru99")

[Link] 1/8
12/13/24, 11:28 PM Python Print() Statement: How to Print with Examples

Run

Output:
Welcome to Guru99

In Python 2, same example will look like

This code is editable. Click Run to Execute

1 print "Welcome to Guru99"

Run

Example 2:
If you want to print the name of five countries, you can write:

This code is editable. Click Run to Execute

[Link] 2/8
12/13/24, 11:28 PM Python Print() Statement: How to Print with Examples

1 print("USA")
2 print("Canada")
3 print("Germany")
4 print("France")
5 print("Japan")

Run

Output:

USA
Canada
Germany
France
Japan

RELATED ARTICLES
→ Multithreading in Python with Example: Learn GIL in Python
→ Go Vs. Python: What’s the Difference?
→ How to Remove Duplicates from a List in Python
→ Palindrome Program in Python

How to print blank lines

[Link] 3/8
12/13/24, 11:28 PM Python Print() Statement: How to Print with Examples

Sometimes you need to print one blank line in your Python program. Following is
an example to perform this task using Python print format.

Example:
Let us print 8 blank lines. You can type:

This code is editable. Click Run to Execute

1 print (8 * "\n")

Run

or:

This code is editable. Click Run to Execute

1 print ("\n\n\n\n\n\n\n\n\n")

[Link] 4/8
12/13/24, 11:28 PM Python Print() Statement: How to Print with Examples

Run

Here is the code


This code is editable. Click Run to Execute

1 print ("Welcome to Guru99")


2 print (8 * "\n")
3 print ("Welcome to Guru99")

Run

Output

Welcome to Guru99

Welcome to Guru99

Print end command


[Link] 5/8
12/13/24, 11:28 PM Python Print() Statement: How to Print with Examples

By default, print function in Python ends with a newline. This function comes with a
parameter called ‘end.’ The default value of this parameter is ‘\n,’ i.e., the new line
character. You can end a print statement with any character or string using this
parameter. This is available in only in Python 3+

Example 1:

print ("Welcome to", end = ' ')


print ("Guru99", end = '!')

Output:
Welcome to Guru99!

Example 2:
# ends the output with ‘@.’

print("Python" , end = '@')

Output:
Python@

You Might Like:

Top
Top Python PyQt5 Tutorial
Tutorial
Interview with Examples:
Examples: Multithreading in
Questions and… Design…
Design… Python with…

[Link] 6/8
12/13/24, 11:28 PM Python Print() Statement: How to Print with Examples

Import module in
Python
Python with
Examples
Examples

Prev Report a Bug Next

Do you think
AI is future of
Work?
Yes No

About
About Us
Advertise with Us
Contact Us

[Link] 7/8
12/13/24, 11:28 PM Python Print() Statement: How to Print with Examples

Career Suggestion
SAP Career Suggestion Tool
Software Testing as a Career
Xero
RemotePC
QuickBooks

Interesting
eBook
Blog
Quiz
SAP eBook
Privacy Manager

English © Copyright - Guru99 2024 Privacy Policy |


Affiliate Disclaimer | ToS | Editorial Policy

[Link] 8/8

Common questions

Powered by AI

Mastering the Python print() function significantly enhances programming efficiency by allowing clearer and more deliberate output management. Effective use of its parameters, like customizing separators and line endings, supports debugging, user interactions, and data presentation without extensive overhead. In practice, the capability to quickly format and update output reduces errors and enhances readability in typical coding practices .

The Python print() function allows customization of the output by using parameters like 'sep' and 'end'. The 'sep' parameter specifies the separator between multiple objects, with a default value of a single space ' '. The 'end' parameter determines what is printed at the end of the output, defaulting to a newline character '\n'. For example, using print('Hello', 'World', sep='-', end='!') will result in the output 'Hello-World!' .

Python's print() function with parameters like 'sep' and 'end' offers a straightforward approach to output formatting, similar to printf in C or System.out.format in Java. These allow control over how data is structured during output, although implementations differ—Python simplifies by handling formatting through intuitive, high-level constructs without explicit format specifiers, unlike C, allowing for easier and more readable code .

A scenario where the 'end' parameter is essential is when creating a log file or console output that shows progress on a single line (e.g., a percentage loading bar). Using print('Progress:', end=' ') and print('50%', end='\r') will show updating progress on the same line. Without using the 'end' parameter, each print would end with a newline, and the output would span multiple lines, making it harder to track .

To display a loading process with updates on the same line using the print() function, the 'end' parameter can be set to '\r' (carriage return), which returns the cursor to the start of the line. For example: import time; for i in range(101): time.sleep(0.1); print(f'Loading {i}%', end='\r'). This will overwrite the output on the same line, giving an appearance of a dynamic loading bar .

The 'sep' parameter in the print() function can be used to customize the separator between multiple arguments, which can be helpful in formatting outputs such as CSV files. For example, using print('apple', 'banana', 'cherry', sep=',') will produce the output 'apple,banana,cherry'. This can simplify the process of generating list-like outputs with different separators .

In Python 3, the syntax for printing a string involves using the print() function with the desired string enclosed in parentheses, such as print('Hello, World'). This differs from Python 2, where print was a statement and could be used without parentheses, e.g., print 'Hello, World' .

In Python 2, the print statement can be used without parentheses, such as print 'Hello, World'. However, in Python 3, print has become a function and requires parentheses: print('Hello, World'). This change allows for more flexibility and consistency, particularly in supporting additional parameters like 'sep' and 'end' .

To print multiple blank lines using the Python print() function, one can multiply the newline character '\n' by the number of desired lines, or simply use multiple print statements with '\n'. For example, print(8 * '\n') or print('\n\n\n\n\n\n\n\n') will output eight blank lines .

The 'end' parameter in Python 3's print() function specifies what character or string should be printed after the current print statement finishes. By default, 'end' is set to '\n', meaning each print statement ends with a newline, creating separate lines. Changing 'end' to a space or any other string will append that character to the output instead of a newline. For instance, using print('Hello', end=' ') followed by print('World!') results in 'Hello World!' as both outputs are concatenated on the same line .

You might also like