Per Erik Strandberg /cv /kurser /blog

I always forget how string formatting works - I constantly look it up. So that is a great reason to make a little summary of it.

Datetime

See also the python Docs [1]

from datetime import datetime
n = datetime.now()

formats = [ ( '%a', "abbreviated weekday" ),
            ( '%A', "full weekday" ),
            ( '%b', "abbreviated month" ),
            ( '%B', "full month" ),
            ( '%c', "appropriate date and time representation" ),
            ( '%d', "day [01,31]" ),
            ( '%H', "hour [00,23]" ),
            ( '%I', "hour [01,12]" ),
            ( '%j', "day [001,366]" ),
            ( '%m', "month [01,12]" ),
            ( '%M', "minute [00,59]" ),
            ( '%p', "AM or PM" ),
            ( '%S', "second [00,61]" ),
            ( '%s', "seconds since 1970-01-01 00:00:00 UTC (*nix only?)" ),
            ( '%U', "week (Sunday as the first day of the week) [00,53]" ),
            ( '%W', "week (Monday as the first day of the week) [00,53]" ),
            ( '%w', "weekday [0(Sunday),6]" ),
            ( '%x', "date representation" ),
            ( '%X', "time representation" ),
            ( '%y', "year without century [00,99]" ),
            ( '%Y', "year" ),
            ( '%Z', "time zone name" ),
            ( '%%', "'%' character" ),
            ( '%Y-%m-%d %H:%M:%S', "My typical date format"),
            ( '%Y%m%d_%H%M%S', "Another favourite") ]

for ( item, explanation ) in formats:
    print "%s - %s - %s" % ( item, n.strftime( item ), explanation )

%a - Thu - abbreviated weekday
%A - Thursday - full weekday
%b - Feb - abbreviated month
%B - February - full month
%c - Thu Feb 16 10:29:28 2012 - appropriate date and time representation
%d - 16 - day [01,31]
%H - 10 - hour [00,23]
%I - 10 - hour [01,12]
%j - 047 - day [001,366]
%m - 02 - month [01,12]
%M - 29 - minute [00,59]
%p - AM - AM or PM
%S - 28 - second [00,61]
%s - 1329384568 - seconds since 1970-01-01 00:00:00 UTC (*nix only?)
%U - 07 - week (Sunday as the first day of the week) [00,53]
%W - 07 - week (Monday as the first day of the week) [00,53]
%w - 4 - weekday [0(Sunday),6]
%x - 02/16/12 - date representation
%X - 10:29:28 - time representation
%y - 12 - year without century [00,99]
%Y - 2012 - year
%Z -  - time zone name
%% - % - '%' character
%Y-%m-%d %H:%M:%S - 2012-02-16 10:29:28 - My typical date format
%Y%m%d_%H%M%S - 20120216_102928 - Another favourite

Basic Numbers

number = 257

print "%%", "No argument is converted a '%' char."
print

print "%r" % number, "String (converts any python object using repr())."
print "%s" % number, "String (converts any python object using str())."
print

print "%d", "%d" % number, "Signed integer decimal."
print "%o", "%o" % number, "Unsigned octal."
print "%x", "%x" % number, "Unsigned hexadecimal (lowercase)."
print "%#o", "%#o" % number, "Unsigned octal."
print "%#x", "%#x" % number, "Unsigned hexadecimal (lowercase)."
print

for char in [ 0x41, 0x2F, '*' ]:
    print "%c", "%c" % char, "Single character (accepts integer or single chara\
cter string)."
print

for number in [ 66666, 1234.666 * 1234.777 ]:
    print "%e", "%e" % number, "Floating point exponential format (lowercase)."
    print "%f", "%f" % number, "Floating point decimal format."
print

print "%g Floating point format. Uses exponential format if exponent is greater\
 than -4 or less than precision, decimal format otherwise."
for number in [ 66666, 1234.666 * 1234.777 ]:
    print "%g", "%g" % number,
print

%% No argument is converted a '%' char.

257 String (converts any python object using repr()).
257 String (converts any python object using str()).

%d 257 Signed integer decimal.
%o 401 Unsigned octal.
%x 101 Unsigned hexadecimal (lowercase).
%#o 0401 Unsigned octal.
%#x 0x101 Unsigned hexadecimal (lowercase).

%c A Single character (accepts integer or single character string).
%c / Single character (accepts integer or single character string).
%c * Single character (accepts integer or single character string).

%e 6.666600e+04 Floating point exponential format (lowercase).
%f 66666.000000 Floating point decimal format.
%e 1.524537e+06 Floating point exponential format (lowercase).
%f 1524537.179482 Floating point decimal format.

%g Floating point format. Uses exponential format if exponent is greater than -4 or less than precision, decimal format otherwise.
%g 66666 %g 1.52454e+06


Belongs to Kategori Programmering