print() in Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 19:- print() in Python

In Python, print() is a built-in function used to display output on the console or terminal. It allows you to print text, variables, and other data to the screen, making it a fundamental tool for debugging and displaying information to users.

The syntax of the print() function is as follows:

pythonCopy code

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Here's what each argument means:

  • objects: One or more objects (variables, strings, numbers, etc.) that you want to print. You can pass multiple objects separated by commas, and print() will display them with a space between each object by default.
  • sep: Specifies the separator between the objects when printing multiple objects. The default separator is a space character ' '.
  • end: Specifies the string to be appended after all the objects have been printed. The default is a newline character '\n', which means each print() call ends with a new line. You can change it to any string you want.
  • file: Specifies the file-like object where the output should be written. By default, it writes to the standard output, which is the console.
  • flush: If True, the output is forcibly flushed. Flushing means the output is written immediately to the file or terminal rather than being buffered. The default is False.

Examples:

  1. Printing a simple message:

pythonCopy code

print("Hello, World!")  # Output: Hello, World!

  1. Printing multiple objects with a custom separator and ending:

pythonCopy code

name = "Alice" age = 25 print("Name:", name, "Age:", age, sep=" - ", end=" years old\n") # Output: Name: Alice - Age: 25 years old

  1. Printing numbers with formatting:

pythonCopy code

x = 3.14159 y = 42 print(f"x = {x:.2f}, y = {y:04d}") # Output: x = 3.14, y = 0042

The print() function is highly versatile and can be used to print various types of data. It is often used for debugging purposes, displaying user messages, formatting output, and more. You can customize its behavior using the optional arguments as needed.

3. Input And Output In Python

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?