Sprintf Function in Python

2010
21
Mar

The printf or sprintf function is used to format strings in PHP and C programming constructs. sprintf in php returns the formatted string whereas printf just prints the output.

$variable = sprintf("%d %s %d = %d",4,"+",5,9);
print $variable;
//OR
printf("%d %s %d = %d",4,"+",5,9);
 
//Both lines are identical.
//Prints 4 + 5 = 9

On python, sprintf is handled using an operator instead of function. The % operator on a string does formatting.

var = '%d %s %d = %d' % (4,'+',5,9)

The above code assigns var = "4 + 5 = 9"

Install Python for Nokia E63 / E71 compatible symbian phones and code on the move
Python rocks Smiling

Similar Posts
Related Searches




Comments

Post new comment

The content of this field is kept private and will not be shown publicly.