• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / f-string in Python 3 / f-string in Python 3.6 for formatting strings

f-string in Python 3.6 for formatting strings

April 23, 2018 by cmdlinetips

How to format string with f-strings in Python
String formatting with f-strings in Python

If you are new to Python 3, learning to use f-string, Python’s relatively new string formatting is a lot of fun. f-string, a short name for “formatted string literal”, is a string literal that is prefixed with letter ‘f’ or ‘F’ and is available from Python version 3.6

There are multiple options to format a string in Python. One of them is str.format() method. For example, we can construct a string by keeping a placeholder within curly braces for the variable in a string followed by .format() method containing the variable.

>version =3.6
>'str.format(), String formatting before, Python {}.'.format(version)

would result in

'str.format(), String formatting before, Python 3.6.'

formatting strings with f-string looks a lot similar to str.format() pattern, but a lot simpler. ‘f’ character at the beginning of the string literal is similar to how Python uses ‘b’ to represent byte string or ‘r’ prefix for raw string.

The ease of f-string is you start the string that you want to format with f and followed by the string. Any variable you want to use can be specified by the variable name within curly braces inside the string. Here is a simple example using f-string with a variable.

>version = 3.6
>f'f-string is available in Python {version} or above.'
'f-string is available in Python 3.6 or above.'

If you want to format a string such that a variable should have single quotes, you can use f-string as follows

version = "3.6"
f"f-string is available in Python {repr(version)} or above."

and the output will be

"f-string is available in Python '3.6' or above."

The beauty of f-string is that it is rather versatile. It can work expression and functions. For example, let us use f-string and inside a string let us call a function and use the result to format a string.

>import math
>a = 10
># define a function
>def my_sqrt(n):
    return math.sqrt(n)
># use the function in f-string 
>f'Square root of {a} is {my_sqrt(a)}.'
'Square root of 10 is 3.1622776601683795.'

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to Use Lambda Functions in Python? Default ThumbnailHow to Sort Lists in-place in Python? Default ThumbnailString Manipulations in Pandas Default Thumbnail9 Ways to Manipulate Strings in Python

Filed Under: f-string in Python 3, Python, Python 3 Tagged With: f-string in Python 3, Python 3, string formatting in Python 3

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version