Python's Power Tool : Print()
Introduction :
When it comes to Python Language , print is one of the very widely used function that you use either in terms of debugging or showing the result on the terminal. Lo and behold ,It's easy to use and very helpful for programmers at any level. To use Print well, it's good to learn more about how it works and what it can do. In this article , we will get to see what the structure of it is and how it is used.
The Structure of Print statement :
First and foremost, we need to understand what print is. print is a built-in function, meaning it's already defined with its own properties. We use it to pass arguments to the parameters, displaying the desired result on the terminal.
For example, consider the following use of the print() function in Python:
print('Hello World')
#Output --> It basically shows Hello World without quotes
We first call the print function, passing the string 'Hello World' as an argument. In Python, strings are sequences of characters enclosed in quotes, either single (') or double ("). The terminal displays the text 'Hello World' without the quotes, as they are used in the code only to define the start and end of the string.
What if we want to use a variable instead of a direct string?
say_hello = 'hello World'
print(say_hello)
#Output --> the output is the same as the other one.
Alternatively, we can perform both actions simultaneously: assigning a value to a variable and then printing it to the terminal. It would look like this:
Handling Multiple Objects:
The print function can also handle multiple objects. The syntax is:
print(*objects, sep=' ', end='\n')
When dealing with multiple objects, we have different ways to concatenate them. For instance:
myfavsport = 'Football'
print('my favourite sport is' , myfavsport)
#Output --> my favourite sport is Football
Here, a space is automatically inserted due to the separator. Alternatively:
领英推荐
myfavsport = 'Football'
print('my favourite sport is ' + myfavsport)
#Output --> my favourite sport is Football
In this case, you need to add the space yourself when using + to concatenate.
2 - sep --> The sep parameter is a space by default, separating one object from another.
As a case in point :
first_name = 'Amir'
family_name = 'Behtar'
print(first_name , family_name , sep = ' ')
#Output --> Amir Behtar
Here, I added a space inside the separator to separate the first and family names.
3 - end -->The end parameter controls the end of the print statement. By default, print ends with a newline character (\n), meaning each call starts on a new line. This can be changed with the 'end' parameter.
For Example :
print("This book is", end=" ")
print("Extraordinarily good")
#Output --> This book is Extraordinarily good
If you remove \n from the end parameter , it will allow the second print to be added to the previous one without a space . If you wanna put a space between the first and second one , you would need to put a space inside the end's value.
F-string :
Moreover , An f-string in Python is a way of putting variables into a string by placing them inside curly braces {}.
x = 10
y = 5
result = f"The sum of {x} and {y} is {x + y}."
print(result)
#Output --> the sum of 10 and 5 is 15.
Finally, using print() by itself adds an empty line, creating a space between the lines before and after it.
print('this guy coming from' , end = '')
print()
print('North America')
#Output --> this guy coming from North America
To Conclude :
the print function is a key tool in Python, useful for beginners and experts alike. It's simple yet powerful, allowing you to display text, variables, and more. With features like formatting and custom end characters, it's essential for debugging and showing results in your code. Becoming deft in print can greatly improve your Python programming skills.
PHD Management
7 个月Thank you for your article
Founder & CEO, Group 8 Security Solutions Inc. DBA Machine Learning Intelligence
8 个月Gratitude for your contribution!