print function in Python - Ryzen Hunt

print function

The print function in Python

The print() function is used to print the specified object (or expression) to the standard output device (screen).

The object can be a string or any other object, the object will be converted into a string before written to the screen.

There are three ways to print any object or expression in python:

  1. print()
  2. f-string
  3. str.format()

print() :

This is the simplest way of printing any object in python. To print any expression or object, we just put that in parenthesis.

For example:

>>>print("Ryzen Hunt")
Ryzen Hunt

>>>print(190)
190

>>>a = "Python tutorial"
>>>print(a)
Python tutorial

f-string:

f-strings stand for formatted strings. This is a new type of string formatting introduced in Python 3.8.1.
f-strings is basically a literal string prefixed with ‘f’ with expressions enclosed in braces ( curly brackets ). These expressions on the execution of the code will be replaced by values. That’s why this type of string formatting is also called as Literal String Interpolation.

For example:

>>>name = "Anikesh"
>>>age = 20
>>>print(f "{name} is {age} years old.")
Anikesh is 20 years old.


str.format():

str.format()  is one of the  string formatting methods  which allows us  to concatenate elements within a string through positional formatting. 
To use this function, we put one or more placeholders defined by a pair of curly braces  { }  into a string and calling the str.format(). The value we wish to put into the placeholders and concatenate with the string is passed as parameters into the format function.
Syntax: {}.format(value)

Note: (value) can be an integer, floating-point number, string, characters, or even variables.

For example:

>>>print("{} is my country." .format("India"))
India is my country.

>>>print("{0} has {1} mangoes." .format("James" , 2))
James has 2 mangoes.

You can change the positional digits in {} to replace the value. For example:

>>>print("{1} has {0} mangoes." .format("James" , 2))
2 has James mangoes.

>>>a = 56
>>>b = 68
>>>c = a+b
>>>print("The sum of {0} and {1} is {2}." .format(a,b,c))
The sum of 56 and 68 is 124.



Hope you learned something new here. Please share this with your friends and Don't forget to share your comments and valuable suggestions and feedback.
For visiting our Python Course series click here, and for HTML course series click here.



Suggested Product: If you dream to be a ethical hacker but do not have any desktop or laptop. Do be sad, here I got a great deal for you, that will help you to master in hacking using only your android mobile.
"Ethical Hacking With Termux" written by a Certified Ethical Hacker Prahlad Shukla. Hope you like the book. This is for the beginner level. This book will teach you the best hacking techniques using your smartphone.
To purchase this book a very low price, click here or on the given image.

You may like these posts

1 comment

  1. Nitin Suryavanshi
    Great information
  1. To insert a code use <i rel="pre">code_here</i>
  2. To insert a quote use <b rel="quote">your_qoute</b>
  3. To insert a picture use <i rel="image">url_image_here</i>