Dark theme

String formating


There are an inordinate number of ways of formating strings in Python; the community is continually picking away at it like an itchy scab. Here's a brief summary of the major ones.

String methods

Strings include a variety of methods that produce new strings with different formatting. They work on string literals and string variables (a literal is just a string without a name), and they are conservative (that is, they don't change the string they are called from &ndash you need to assign the result to a new variable to keep it). Such functions including:

print("Hello World".upper()) # HELLO WORLD
print("Hello World".lower()) # hello world
print("Hello World".capitalize()) # Hello world
print("-".join(["Hello", "World"]) # Hello-World

Some of the most complicated work done with strings is ensuring non-US/UK characters appear correctly as computing has only just started to cope with them. This includes non-European language character sets and things like emoticons. For a good introduction to this, see Fluent Python on the textbook list.


Print

The print command is core to outputting text to the screen, and even its most basic uses include some formatting. For example:

>>> a = "hello"
>>> b = "world"
>>> print(a, b)
hello world >>> print(a + b)
helloworld

In scripts, you can also set alternative ends to print lines (replacing the newline). For example

print("a", end=",")
print("b", end=",")
print("c")

Prints:

a,b,c

You can print multple objects in this form using:

print(*objects, sep='', end='\n', file=sys.stdout, flush=False)

This prints objects to a file (or stout), separated by sep and followed by end. Other than objects, everything must be a kwarg as everything else will be written out.

The real power comes with formating instructions though...


Formatting instructions


Old Style (info)

Substitutes % for variables with formatting markup.

Example:
print('%(a)s has: %(b)10.2f pounds'%{'a':'Bob','b':2.23333})

Output:
Bob has:          2.23 pounds

Not very resilient to errors in markup, which are easy to make.
In the above, 10 is the space distance to the last number character (zero indexed), while 2 is the decimal points.


Templating (info)

More basic: allows the construction of a formatted template into which variables can be linked.

Example:
import string
t = string.Template('${a} has:          $b pounds.')
t.substitute(a='Bob', b=2.23333)

Output:
Bob has:          2.23333 pounds

Simple substitution, but hard to mess up (there's a "safe_substitute" that copes with missing values, for example).


Formatted string literals (3.6+) (info)

Similar, to Old Style but less error prone.

Example:
a = "Bob"
b = 2.23333
print( f"{a} has: {b:10.3} pounds." )

Output:
Bob has:          2.23 pounds

Note that escape characters have to be in variables rather than inline.
3, here, is the total characters in the number (up to the number in the original).


str.format (info)

Syntax mixes formatted string literals and Old Style.

Example:
a = "Bob"
b = 2.23333
print( "{0} has: {1:10.2f} pounds".format(a,b) )

Output:
Bob has:          2.23 pounds

Recommended for the moment. The markup for this can be found in the documentation.