loading...

Python f strings

khansubhan95 profile image Mohammed Subhan Khan Originally published at mskhan.in on ・1 min read

Python f strings allow for string interpolation similar to Swift or JavaScript. They are a new feature introduced in Python 3.6. Let’s look at an example

hello = 'hello'
world='world'
print(hello + ' ' + world)

You can do a similar thing using f strings as follows

hello = 'hello'
world='world'
print(f'{hello} {world}')

String interpolation allows to embed variables into the string. This is especially useful because it does away with casting data types into strings to embed in a string. f strings even allow to embed expressions in {}

x=5
print(f'I have {x+1} apples!')

Usage : f followed by single or double quotes.

Discussion

markdown guide