Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left

  Apply Now

Python Foundations - Assessments

38 / 54

Format Operator

The format operator % works on strings for variable substitution purposes. When applied to integers, % is the modulus operator. But when the first operand is a string, % is the format operator.

a = 73
b = "%d" % a
print (b)

It prints 73. Basically, it substituted %d with the value of the variable a. the format sequence %d means that the second operand should be formatted as an integer ("d" stands for "decimal").

For more than one format sequence in the string, the second argument has to be a tuple. Each format sequence is matched with an element of the tuple, in order.

The following example uses %d to format an integer, %g to format a floating-point number, and %s to format a string:

b =  "%d is an integer, %g is floating-point and %s is string" % (23, 23.34, "Twenty-Three")
print(b)

It prints '23 is an integer, 23.34 is floating-point and Twenty-Three is string'

The number of elements in the tuple must match the number of format sequences in the string. The types of elements also must match the format sequences.

INSTRUCTIONS

Enter the output of the following statements: Execute the statements in the notebook to check the output

  • print('%d+3' % 3)
  • Is print("%f %f %s") valid Python statement? Enter True or False
  • Is print("%f %s %d" % (12.3, 12, 'cloudxlab') valid Python statement? Enter True or False

No hints are availble for this assesment

Answer is not availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...