1.4 String Formatting
1.4 String Formatting
(https://www.udemy.com/user/joseportilla/)
String Formatting
String formatting lets you inject items into a string rather than trying to chain items together using commas or
string concatenation. As a quick comparison, consider:
player = 'Thomas'
points = 33
Since you will likely encounter all three versions in someone else's code, we describe each of them here.
In [1]:
You can pass multiple items by placing them inside a tuple after the % operator.
localhost:8888/lab 1/7
10/12/2020 03-Print Formatting with Strings
In [2]:
I'm going to inject some text here, and more text here.
In [3]:
x, y = 'some', 'more'
print("I'm going to inject %s text here, and %s text here."%(x,y))
I'm going to inject some text here, and more text here.
In [4]:
In [5]:
The %s operator converts whatever it sees into a string, including integers and floats. The %d operator
converts numbers to integers first, without rounding. Note the difference below:
In [6]:
localhost:8888/lab 2/7
10/12/2020 03-Print Formatting with Strings
In [7]:
In [8]:
In [9]:
In [10]:
In [11]:
Multiple Formatting
Nothing prohibits using more than one conversion tool in the same print statement:
In [12]:
localhost:8888/lab 3/7
10/12/2020 03-Print Formatting with Strings
For example:
In [13]:
In [14]:
In [15]:
In [16]:
localhost:8888/lab 4/7
10/12/2020 03-Print Formatting with Strings
In [17]:
Fruit | Quantity
Apples | 3.0
Oranges | 10
By default, .format() aligns text to the left, numbers to the right. You can pass an optional < , ^ , or > to set
a left, center or right alignment:
In [18]:
In [19]:
Field widths and float precision are handled in a way similar to placeholders. The following two print statements
are equivalent:
In [20]:
localhost:8888/lab 5/7
10/12/2020 03-Print Formatting with Strings
Note that there are 5 spaces following the colon, and 5 characters taken up by 13.58, for a total of ten
characters.
Introduced in Python 3.6, f-strings offer several benefits over the older .format() string method described
above. For one, you can bring outside variables immediately into to the string rather than pass them as
arguments through .format(var) .
In [21]:
name = 'Fred'
In [22]:
Where with the .format() method you might see {value:10.4f} , with f-strings this can become {value:
{10}.{6}}
In [23]:
num = 23.45678
print("My 10 character, four decimal number is:{0:10.4f}".format(num))
print(f"My 10 character, four decimal number is:{num:{10}.{6}}")
In [ ]:
localhost:8888/lab 6/7
10/12/2020 03-Print Formatting with Strings
Note that with f-strings, precision refers to the total number of digits, not just those following the decimal. This fits
more closely with scientific notation and statistical analysis. Unfortunately, f-strings do not pad to the right of the
decimal, even if precision allows it:
In [24]:
num = 23.45
print("My 10 character, four decimal number is:{0:10.4f}".format(num))
print(f"My 10 character, four decimal number is:{num:{10}.{6}}")
If this becomes important, you can always use .format() method syntax inside an f-string:
In [25]:
num = 23.45
print("My 10 character, four decimal number is:{0:10.4f}".format(num))
print(f"My 10 character, four decimal number is:{num:10.4f}")
localhost:8888/lab 7/7