- What is the output statement?
Printing the data to the console is known as the output statement. - Python Output statement prints any statement to the output window.
- If you want to print some data to console then we can use the print() function in python.
- Python print statement is more flexible than in other programming languages.
- There are various forms for the print statement which are as follows
Form 1
- Python print statement without any argument.
- Print Syntax
Print() - This is used to print the blank line.
- If you want line separator we use print statement.
- Python print example
Print() => this will print blank line.
Form 2
- Python Print statement with the string argument.
- Print Syntax
Print(string)
Where string is an argument with string type - Python print string
Print(“Python”) => this will print string python - We can use escape character also
Example
Print(“python \n programming “) => this will print python in one line and programming in the new line.
- We can use + operator and * operator in print statement.
- + operator in print statement
+ operator is used for string concatenation
We allow using + (concatenation) operator in between string.
Whenever we use + both arguments should be str type.
Example
Print(“python”+”programming “)
=> this will print pythonprogramming
When we use + in between string, then both strings are concatenated. - *operator in print statement
We can use the * operator in print statement.
Example
Print(“python “*2)
This will printpythonpythonpython
Whenever we use* operator one argument should be str and others should be interested.
The * operator will print string the number of times we mentioned. - We can take multiple arguments in the print statement.
Example
Print(“python”+”programming “) =>pythonprogramming. - Whenever we use+ operator for multiple argument printing then + in between both arguments there will not be space added.
- To print space between argument use, (comma)
Example
Print(“python”, “programming “) - Whenever we use, operator while printing multiple arguments then there will be space get added in between both arguments.
Form 3
- Python Print statement with the variable number of arguments.
- We can use any number of arguments in print function.
- Example
A, B, C=10, 20,30
Print(“the values are=”, A, B, C) =>the values are= 10 20 30 - By default, while printing, the default separator is space.
- If you want different separator instead of space, you can do this using the sep attribute.
- September attribute is the separator attribute.
- You can add separator attribute in print function.
- Syntax
Print(values, sep=”sep attribute “) - Python print with the separator
12345print("Print with sep attribute")A=10B=20C=30print(A,B,C,sep=":")
Output:- - Print statement with end attribute
By default in python print() added at a new line.
Example
12print("python")print("programming") - Output:-
python print See in above example print() by default adds a new line for each line.
- Now if you don’t want to print a new line, then use end attribute.
- End attribute Syntax
Print(values, end=” end attribute “) - Python print without newline
12print("python",end=" ")print("programming") - Output:-
python print without newline - Note:- Separator attribute is for the separator in between arguments and end attribute for separator at the end.
- Python print example
-
123print("print with seperator : an d end attribute")print(10, 20,30,sep=",", end="......")print(90, 100,sep=":")
- Output:-
Form 4
- Print statement to print object.
- You can pass any object to print statement.
- Syntax
Print(object)
Where object can be the list or set or any object. - Example
123print("print object")list=[10,20,30]print(list) - Output:-
Form 5
- Print statement with formatted string.
- Formatted strings are
%iand%d=>int type
%f => float type
%s =>string type - How can use formatted strings
Syntax
Print(“formatted string” %(variables))
Where there is a space between the formatted string in double quote and %variablelist. - Example print formatted string python
A=10
Print(“value of A is%i” %A) => value of A is 10
In the above example in%i the value of A is printed. - You can print multiple formatted strings in a single print statement.
- Example
A, B, C = 10,20,30
Print(“A value is%i and B value is%i and C value is%i” %(A, B, C))
= A value is 10 and B value is 20 and C value is 30 - You can also print arguments with different formatted string in a single print statement.
Example
Name = “python”
L=[10, 20]
Print(“Name is%s and given list is%d” %(Name, L))
= Name is python and given list is [10, 20]
In the above example, we printed one string type and one list.
Print() function with replacement operator
- Replacement operator is {}
- Syntax
Print(“formatted string {index}”.format(variable list)) - Where format() contains a list of a variable.
{Index} is the replacement operator with the index which got replaced with an associated variable. - Example
Name = “python”
Fees = 1000
Print(“course name={0} and it’s fees ={1}”.format(name, fees))
In above example, replacement operator are replaced with values of variable Specified in format().
{0} is replaced by name
And {1} is replaced by fees
Result we get is
Course name=python and it’s fees=1000 - In case, you are not specifying index but default it considers index.
Example
Name = “python”
Fees = 1000
Print(“course name={} and it’s fees ={}”.format(name, fees))
In the above example, replacement operator is replaced with values of variable Specified in format().
{} is replaced by name
And {} is replaced by fees
Result we get is
Course name=python and it’s fees=1000 - If you are not specifying an index in replacement operator {} then the order of the variable list is important.
- Another option is, you can write identifier in {} and format order is not important.
Example
Name = “python”
Fees=1000
Print(“course name={x} and it’s fees ={y}”.format(y=Name, x=fees))
In above example, replacement operator are replaced with values of variable Specified in format().
{x} is replaced by name
And {y} is replaced by fees
Result we get is
Course name=python and it’s fees=1000
Leave a Reply