#!/usr/bin/python # string str = "String: %s %s" % ('One', 'Two'); str = "String: %10s %10s" % ('One', 'Two'); # One Two str = "String: %-10s %-10s" % ('One', 'Two'); #One Two str = "String: %10.2s %10.2s" % ('One', 'Two'); # On Tw # number str = "Number: %d %f" % (10, 3.14); # 10 3.140000 str = "Number: %10d %10.2f" % (10, 3.14); # 10 3.14 str = "Number: %010d %010.2f" % (10, 3.14) #0000000010 0000003.14 # named placeholder d = {'name':'Lin', 'age':37}; str = "%(name)s %(age)d" % d;
#!/usr/bin/python # string str = "String: {} {}".format('One', 'Two'); #One Two str = "String: {1} {0}".format('One', 'Two'); #Two One str = "String: {:>10} {:>10}".format('One', 'Two'); # One Two str = "String: {:^10} {:^10}".format('One', 'Two'); # One Two str = "String: {:<10} {:<10}".format('One', 'Two'); #One Two str = "String: {:_<10} {:_<10}".format('One', 'Two'); #One_______ Two_______ str = "String: {:>10.2} {:>10.2}".format('One', 'Two'); # On Tw # number str = "Number: {:d} {:f}".format(10, 3.14); # 10 3.140000 str = "Number: {:10d} {:10.2f}".format(10, 3.14); # 10 3.14 str = "Number: {:010d} {:010.2f}".format(10, 3.14) #0000000010 0000003.14 # named placeholder d = {'name':'Lin', 'age':37}; str = "{name} {age}".format(**d); print str