Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a C format in python

A python newbie question:

I would like to print in python with the c format with a list of parameters:

agrs = [1,2,3,"hello"]
string = "This is a test %d, %d, %d, %s"

How can I print using python as:

This is a test 1, 2, 3, hello

Thanks.

like image 475
KTran Avatar asked Sep 19 '26 19:09

KTran


1 Answers

Strings overload the modulus operator, %, for printf-style formatting, and special case tuples for formatting using multiple values, so all you need to do is convert from list to tuple:

print(string % tuple(agrs))
like image 79
ShadowRanger Avatar answered Sep 21 '26 11:09

ShadowRanger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!