How to print like printf in python

Ads
 

How to print like printf in python

Hi,

I am from C background and there used to be printf function. Jow to print like printf in python? The printf function is C/C++ is very useful. So, looking for some line printf in Python language.

Thanks

View Answers

February 16, 2019 at 4:54 AM

Hello,

In C/C++ printf function is used for formatted output. You can easily format the data and print.

In Python you can use print function with format option to achieve the same.

Here in following example we are going to define a float variable and then print this variable in 2 and 4 decimal places.

i = 98.48359
# Format value in 2 decimal places
print("Value in 2 decimal places: {:.2f}".format(i))

# Format value in 4 decimal places
print("Value in 4 decimal places: {:.4f}".format(i))

Above program gives following output:

>>> 
>>> 
>>> i = 98.48359
>>> # Format value in 2 decimal places
... print("Value in 2 decimal places: {:.2f}".format(i))
Value in 2 decimal places: 98.48
>>> 
>>> # Format value in 4 decimal places
... print("Value in 4 decimal places: {:.4f}".format(i))
Value in 4 decimal places: 98.4836
>>>

Thanks


February 16, 2019 at 5:41 AM

Hi,

Here is screen shot of output of program:

Check more tutorials at:

Python Tutorials - Learn and Master one of most popular programming language

Thanks

Ads









Related Tutorials/Questions & Answers:
For printing out strings, there are echo, print and printf. Explain the differences.
For printing out strings, there are echo, print and printf. Explain the differences.  For printing out strings, there are echo, print and printf. Explain the differences
How to run external command in Python?
How to run external command in Python?  Hi, I am writing an application in Python and there is requirement of calling the external command from... external command. How to run external command in Python? Thanks   Hi
Advertisements
how to print String as pattern ?
how to print String as pattern ?  i have a one String name ="rajesh" i want to print pattern like r * ra ** raj raje rajes rajesh please help me
Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times)
Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times)  Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print
Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times)
Can u print in turbo C  Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times
how to print the server time
how to print the server time  how to print the server time in jsp and update time in fix interval
How to print this Format exactly?
How to print this Format exactly?   * * * & reverse
How to print this in java?
How to print pattern in Java?  How to print a particular pattern in Java...;  How to print this in java
how to print the server time
how to print the server time  i want to print database time in clintside and update time in every secound without creating connection to database for each time. how can i do
printf and println
printf and println  what is the differences between printf and println?   Hi Friend, The println prints the text on a new line, where as printf prints the text on the same line. Thanks
how to print - Java Beginners
how to print  how to print something on console without using System.out.print() method ?  Hi Friend, You can use PrintWriter to write anything on the console. import java.io.*; public class Print{ public static
How to print UIButton id
How to print UIButton id  How to get the id of the button in the IOS application? Thanks   Hi, You can use the following code: - (IBAction)btnClicked:(id)sender { UIButton *button = (UIButton *)sender; int
how to print the following pattern
how to print the following pattern  Hello hEllo heLlo helLo hellO pls reply soon its an emergency   class StringPattern { public static void main(String[] args) { String st="hello
how to print pdf format
how to print pdf format  Hi every body iam doing school project iam using backend as oracle front end java .how to print student marks list /attendence in pdf format. please help me. thanks in advance.   Here
How to call print dialog box through JMenuItem?
How to call print dialog box through JMenuItem?  What is the way to call the print dialog box through JMenuItem to print the current page like we can do in javascript by calling window.print() function
How to design a foot print on the sand, print on the sand, print
How to design a foot print on the sand       You might have seen foot prints on the sand field. Do you think, it can be design in the photoshop, yes I have done here. I have used
how to print JInternal frame component ?
how to print JInternal frame component ?  hello sir, i devalop a swing application .but problem is that how display report & print it. some data prefech from database & keep to jtable but how it is print with table
How can print integer in method
How can print integer in method   a method call process public process(int jobid,int remain){ } how to print int jobid ,int remain in for loop jobid in proc[0],remain in proc[1],both show in text file how to print proc[0
How to send massage like way2sms.com
How to send massage like sms websites  Hi sir how i can make a small project in php (send massage by website like free sms website on internet) Please advice me
How to print current time in r?
How to print current time in r?  Hi, I am learning and want to print current date and time on console. How to print current time in R Programming? Thanks   Hi, You should use Sys.time() for getting current date
How to print the following pattern in java?
How to print the following pattern in java?  How to print the following pattern in java? he he is he is going he is going in   import java.io.*; class StringPattern { public static void main(String[] args
How to translate gameobject like chainwise
How to translate gameobject like chainwise  i try to make program of just like in casino where number move up and down high speed and then its speed... at lower limit so that it look like number follow each other. after some time
how to print of hours and minutes of 8725 seconds
how to print of hours and minutes of 8725 seconds  how to print of hours and minutes of 8725 seconds
how to print all colors using awt
how to print all colors using awt  how to print all colors using awt
How to print a webpage without url and date in jsp ?
How to print a webpage without url and date in jsp ?  How to print a webpage without url and date in jsp
How to pretty print XML from Java?
How to pretty print XML from Java?  How to pretty print XML from Java
how to print headings horizontally in jsp
how to print headings horizontally in jsp  In one of my jsp i have used for loop. in every iteration it will fetch value from db and print it on heading section, it prints vertically but my requirement is to print it horizontally
How to print list of files in client system
How to print list of files in client system  have some list of files in server.i just want to print that files in client machine with out opening the file.i give the option for user like printall.if user click printall prints
how to print from right to left in java????
how to print from right to left in java????   can anyone pls tell how to print from right to left in java
How to Print Array in JavaScript
How to Print Array in JavaScript In this section you will learn about Array...;; There are some methods of array that can use in array like length() can find...=mycity.indexOf["Agra"]//return the index of Agra Now, here is the example how
how to call the print dialog box through JMenuItem
how to call the print dialog box through JMenuItem  What is the way to call the print dialog box through JMenuItem to print the current page...could you please help me by sending code

Ads