In [ ]:
'Hello World'
In [ ]:
name = 'Mike'
'Hello %s' % name
In [ ]:
'Hello World'.format()
In [ ]:
'Hello {}'.format('Mike') # This is the new string replacement format.
In [ ]:
'Hello {} {} in {}'.format('Mike', 'SEO', 'NYC') # Here's the easiest way to feed in a sequence of strings
In [ ]:
'{0}'.format('zero') # You can number the replacement locations to target them.
In [ ]:
'{}'.format('zero') # Many times, you're really just replacing one thing.
In [ ]:
'{}'.format('zero', 'one', 'two') # But if you overload it, only the first one replaces.
In [ ]:
'{} {}'.format('zero', 'one', 'two') # As they implicitly match-up, they will also fill-in.
In [ ]:
'{0} {1} {2}'.format('zero', 'one', 'two') # You can explicitly positionally feed in the replacements.
In [ ]:
'Hello, {foo} and {bar}'.format(bar='eggs', foo='spam') # You can feed the replacements in as name/value argument pairs.
replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
arg_name ::= [identifier | digit+]
attribute_name ::= identifier
element_index ::= digit+ | index_string
index_string ::= <any source character except "]"> +
conversion ::= "r" | "s" | "a"
format_spec ::= <described in the next section>
In [ ]:
'{}'.format(1) # String formatting is actually the best way to FORMAT NUMBERS.
In [ ]:
'{0}'.format(1) # I'm putting the optional index placholder in so that it's clearer as we build up the API.
In [ ]:
'{0:}'.format(1) # After the placeholder, you can put an optional colon for a format_spec
In [ ]:
'{:}'.format(1) # Because the number before colon is an optional placeholder, this works too.
In [ ]:
'{:,}'.format(1000) # There's a whole mini-language here. Here's insertting commas.
format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type]
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= digit+
grouping_option ::= "_" | ","
precision ::= digit+
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
In [ ]:
'{:}'.format(1/3) # Here's the unconstrained default decimal precision of floats.
In [ ]:
'{:.1f}'.format(1/3) # This is floating point
In [ ]:
'{:.1%}'.format(1/3) # Simnply replacing f with % multiplies by 100 and adds percent sign.
In [ ]:
'{:.0%}'.format(1/3) # The number before the % specifies float precision just like with f
In [ ]:
'{:8.0f}'.format(10) # A character IMMEDIATELY AFTER the comma specifies a forced width (if smaller).
In [ ]:
'{:*^8.0f}'.format(10) # Fill with * and center-align
In [ ]:
'{:*<8.0f}'.format(10) # Fill with * and left-align
In [ ]:
'{:0=8.0f}'.format(100) # Pad number with 0
In [ ]:
import datetime
now = datetime.datetime.now() # You can get extreme control over date/times.
now
In [ ]:
'{:%Y-%m-%d %H:%M:%S}'.format(now) # datetime data-types have yet more format_spec rules to learn.
In [ ]:
print('Day: {:%A}'.format(now))
print('Month: {:%B}'.format(now))
print('Day of year: {:%j}'.format(now)) # Lots possible documented https://docs.python.org/3/library/datetime.html|