print() function and input() function make the program more interactive with the user.
Although, there are some extra things we can do with print() which makes the output formattable.
Functionalities of print() function
- Let’s start with the syntax:
print(object(s), sep=separator, end=end, file=file, flush=flush)
- Fact: In Python, all data types are implemented as an object.
- object(s): Will accepts any number of objects, which will be converted into a string before printed. We can pass it in two way:
- Concatenated:
- Comma seperated:
Output:
Enter any number: 2021
year:2021
- sep=’separator’: This is Optional.
Used to specify how to separate the objects, if there is more than one.
Default is ‘ ‘ (space).
Output:
Enter any number: 2021
year: 2021
- end=’end’: This is Optional.
Used to specify what to print at the end.
Default is ‘\n’ (new line).
Output:
Enter any number: 2021
year: 2021 month: Jan
- file: This is Optional. Default is sys.stdout. Used to specify the name of the file to write the output. If the file doesn’t exist, it will create a new file.
Output (in ‘Greetings.txt’ file):
!!! Wish you a happy new year !!!
Enjoy the year--->2021---> and the month --->January
- flush: This is Optional. A Boolean, specifying if the output is flushed (True) or buffered (False). Default is False.
- Check out this GFG article for a detailed explanation of the flush parameter
Best Resources
- Python Blog Series: A Blog series where I will be learning and sharing my knowledge on each of the above topics.
- Learn Python for Free, Get Hired, and (maybe) Change the World!: A detailed roadmap blog by Jayson Lennon (a Senior Software Engineer) with links to free resources.
- Zero To Mastery Course – Complete Python Developer: A comprehensive course by Andrei Neagoie (a Senior Developer) that covers all of the above topics.
Who Am I?
I’m Aswin Barath, a Software Engineering Nerd who loves building Web Applications, now sharing my knowledge through Blogging during the busy time of my freelancing work life. Here’s the link to all of my socials categorized by platforms under one place: https://linktr.ee/AswinBarath
Thank you so much for reading my blog🙂.