Python: Print String

By Xah Lee. Date: .

printf

use print to print any value.

print( 3 )
print(3, "some", [1, 2])

Print string with formating

the print also let you format the string.

The syntax is:

print(str % (arg1, arg2, etc))

# print string with formating

# integer
print("%d" % (1234))
# 1234

# padding by space
print("(%4d)" % (12))
# (  12)

# float. 2 integer, 4 decimal
print("(%2.4f)" % (3.123456789))
# (3.1235)

# string
print("(%5s)" % ("cats"))
# ( cats)

print("(%2s)" % ("cats"))
# (cats)

print("(%2d) (%6d) (%2.4f) (%s)" % (1234, 5678, 3.123456789, "cats!"))
# (1234) (  5678) (3.1235) (cats!)

Python String