String Formatting With F-Strings
Learn about what Python’s formatted string literals (f-strings) are and when to use them. You’ll see that f-strings allow you to use embedded Python expressions inside string constants:
>>> f'Hello, {name}!'
'Hello, Bob!'
This lesson will also show you how to use f-strings for:
- Simple string formatting and
- Inline arithmetic
00:01 Moving on to the third major way of formatting strings: f-strings. f-strings was released in Python 3.6 and it sort of streamlined the way that we format strings now.
00:15 So, when should we use f-strings? As I mentioned, Python 3.6 and later versions support f-strings. And, when it’s not a string generated by a user of your program.
00:28
Let’s hop back into the scenario. Once again, we have the two variables, errno
and name
, that we would like to format into a single string. With f-strings, you just need to prefix the string with the letter f
and embed the expressions you want within curly brackets.
00:48
That looks like this. Follow along with me. That’s it! And give it an exclamation point because we’re happy to see Bob, 'Hello, Bob!'
This method of formatting also allows you to embed arithmetic into your strings.
01:08
Give this example a try. We’re going to give it two variables, a
and b
. a = 5
, b = 10
, and then we’re going to set up our f-string.
01:24
f'Five plus ten is {a + b} and not {2 * (a + b)}'
, and close that off, and print this out. 'Five plus ten is 15 and not 30.'
f-strings also support the formatting or selection of output, so give that a try with me.
02:01
And when it’s time to select hexadecimal, we’re going to do :#x
.
02:14
'Hey Bob, there is a 0xbadc0ffee error!'
So, you can see here how f-strings kind of streamline and make string formatting just a little bit simpler. Less characters to type, and it’s quicker to set up where you want to substitute your variables.
Abby Jones on June 27, 2019
This doesn’t work for me for some reason.
f'Five plus ten is {a + b} and not {2 * (a + b)}.'
Dan Bader RP Team on June 27, 2019
@Abby: What’s the error message you’re getting? The following should work, as long as you’re on Python 3.6 or above:
>>> a = 5
>>> b = 10
>>> f'Five plus ten is {a + b} and not {2 * (a + b)}.'
'Five plus ten is 15 and not 30.'
This formatting feature called f-strings is only available in Python versions 3.6 and up, so if you’re on a different version it won’t work.
Abby Jones on June 28, 2019
I’m using 7, but I was including .format erroneously.
Abby Jones on June 28, 2019
3.7, I mean.
Abby Jones on June 28, 2019
Okay hold up, I just tried it again and it doesn’t work in bpython at all. Syntax error
>>> a=5
>>> b=10
>>> f'Five plus ten is {a + b} and not {2 * (a + b)}.'
File "<input>", line 1
f'Five plus ten is {a + b} and not {2 * (a + b)}.'
^
SyntaxError: invalid syntax
Abby Jones on June 28, 2019
Okay, just verified that bypthon doesn’t allow this, but python prompt does.
Dan Bader RP Team on June 28, 2019
@Abby: You can install bpython
on top of different versions of your local Python install. If bpython
is installed on top of Python 3.6 or above, it will work:
$ bpython
bpython version 0.18 on top of Python 3.7.3 /Users/dbader/.pyenv/versions/3.7.3/bin/python3.7
>>> a=5
>>> b=10
>>> f'Five plus ten is {a + b} and not {2 * (a + b)}.'
'Five plus ten is 15 and not 30.'
Abby Jones on June 28, 2019
Oh jeez, it is on top of 2.7 lol. hangs head in shame
eatyourgreens on Jan. 31, 2021
I don’t understand how you got bad coffee to appear. Is that what the hexnumber gets converted to?
Bartosz Zaczyński RP Team on Feb. 1, 2021
@eatyourgreens Yes, the string “badc0ffee”, which happens to be comprised of hexadecimal digits, represents the integer number 50159747054 stored in the errno
variable. You can convert from both representations to see this for yourself:
>>> hex(50159747054)
'0xbadc0ffee'
>>> int('0xbadc0ffee', 16)
50159747054
Become a Member to join the conversation.
Karl on May 4, 2019
Hey, as a not native english speaker, it is easy to me to understand your speech. Thanks! :-)