You can do this using the str.format() method.

>>> width = 20
>>> print("{:>{width}} : {:>{width}}".format("Python", "Very Good", width=width))
              Python :            Very Good

Starting from Python 3.6 you can use f-string to do this:

In [579]: lang = 'Python'

In [580]: adj = 'Very Good'

In [581]: width = 20

In [582]: f'{lang:>{width}}: {adj:>{width}}'
Out[582]: '              Python:            Very Good'
Answer from Sede on Stack Overflow
๐ŸŒ
DZone
dzone.com โ€บ data engineering โ€บ data โ€บ python string format examples
Python String Format Examples
January 23, 2020 - That brings us to Python's native string format() method. Introduced in Python 3, this method provides a simple way to construct and format strings with dynamic substitutions.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-24481.html
dynamic f-string example
you might want to put this script in your python examples folder. you won't likely ever need to do this, but if some day you need to dynamically change the format of an f-string at run time (normally, python constructs code from an f-string at compi...
๐ŸŒ
Like Geeks
likegeeks.com โ€บ home โ€บ python โ€บ python string interpolation (make dynamic strings)
Python string interpolation (Make Dynamic Strings)
November 22, 2023 - This is a powerful feature in Python that enables you to create a dynamic string in Python by embedding or substituting the values of variables into the string at runtime. Python supports multiple ways to format strings and perform string ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-formatting-in-python
String Formatting in Python - GeeksforGeeks
There are five different ways to perform string formatting in Python: Formatting with % Operator.
Published ย  March 18, 2026
๐ŸŒ
Real Python
realpython.com โ€บ python-string-formatting
Python String Formatting: Available Tools and Their Features โ€“ Real Python
December 2, 2024 - String formatting is essential in Python for creating dynamic and well-structured text by inserting values into strings. This tutorial covers various methods, including f-strings, the .format() method, and the modulo operator (%). Each method ...
๐ŸŒ
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
With this site we try to show you the most common use-cases covered by the old and new style string formatting API with practical examples. All examples on this page work out of the box with with Python 2.7, 3.2, 3.3, 3.4, and 3.5 without requiring any additional libraries.
๐ŸŒ
Real Python
realpython.com โ€บ python-formatted-output
A Guide to Modern Python String Formatting Tools โ€“ Real Python
February 1, 2025 - In modern Python, you have f-strings and the .format() method to approach the tasks of interpolating and formatting strings. These tools help you embed variables and expressions directly into strings, control text alignment, and use custom format ...
Find elsewhere
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
Common string operations โ€” Python 3.14.5 documentation
These nested replacement fields may contain a field name, conversion flag and format specification, but deeper nesting is not allowed. The replacement fields within the format_spec are substituted before the format_spec string is interpreted. This allows the formatting of a value to be dynamically ...
๐ŸŒ
Python
peps.python.org โ€บ pep-3101
PEP 3101 โ€“ Advanced String Formatting | peps.python.org
This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing โ€˜%โ€™ string formatting operator.
๐ŸŒ
Real Python
realpython.com โ€บ python-f-strings
Python's F-String for String Interpolation and Formatting โ€“ Real Python
November 30, 2024 - By the end of this tutorial, youโ€™ll ... {}. To include dynamic content in an f-string, place your expression or variable inside the braces to interpolate its value into the string....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ string-formatting-in-python-using
Python Modulo String Formatting - GeeksforGeeks
March 12, 2024 - format() method in Python is a tool used to create formatted strings. By embedding variables or values into placeholders within a template string, we can construct dynamic, well-organized output.
๐ŸŒ
Developmentality
developmentality.wordpress.com โ€บ 2012 โ€บ 05 โ€บ 17 โ€บ hello-planet_name-creating-dynamicstrings-in-python
Hello {planet_name}: Creating strings with dynamic content in Python | Developmentality
May 17, 2012 - would produce โ€œhello worldโ€. Python has this feature built in to strings with the % operator. You can read more in depth about the [String Formatting Operations][] and its syntax, but at the very least you should memorize the flags %d (for integer types), %f (for floating point), and %s (for strings).
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ string-formatting-in-python
Python String Formatting - How to format String? - GeeksforGeeks
format() method in Python is a tool used to create formatted strings. By embedding variables or values into placeholders within a template string, we can construct dynamic, well-organized output.
Published ย  August 21, 2024
๐ŸŒ
sqlpey
sqlpey.com โ€บ python โ€บ top-4-ways-to-dynamically-format-strings-in-python
Top 4 Ways to Dynamically Format Strings in Python - sqlpey
December 6, 2024 - A: You can use the str.format() method, f-strings, or padding techniques with the % operator to dynamically adjust the width of formatted strings.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ format
Python String format()
There's an easier way to format dictionaries in Python using str.format(**mapping). # define Person dictionary person = {'age': 23, 'name': 'Adam'} # format age print("{name}'s age is: {age}".format(**person)) ** is a format parameter (minimum field width). You can also pass format codes like precision, alignment, fill character as positional or keyword arguments dynamically. # dynamic string format template string = "{:{fill}{align}{width}}" # passing format codes as arguments print(string.format('cat', fill='*', align='^', width=5)) # dynamic float format template num = "{:{align}{width}.{precision}f}" # passing format codes as arguments print(num.format(123.236, align='<', width=8, precision=2)) Output ยท
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_string_formatting.asp
Python String Formatting
To format values in an f-string, add placeholders {}, a placeholder can contain variables, operations, functions, and modifiers to format the value. ... A placeholder can also include a modifier to format the value.
๐ŸŒ
Medium
medium.com โ€บ @RohitPatil18 โ€บ string-formatting-in-python-d287c0bcac9e
String Formatting in Python. When I first started programming, Iโ€ฆ | by Rohit Patil | Medium
January 2, 2022 - F-strings are my favourite ones and I used them a lot while programming in python. Instead of passing any arguments to any function we can directly use variables as placeholder surrounded by curly braces. We have to use f at start of the string in order for it to work.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ formatted-strings
Python Formatted Strings / f-string formatting Guide
The :.2f inside the curly braces is a format specifier that formats the number to two decimal places. F-strings are preferred for their readability, conciseness, and performance. f-strings use a straightforward syntax. To create an f-string, prefix a string literal with f and include any Python expression inside curly braces ({ and }): ... f-strings can incorporate advanced string methods, enabling dynamic transformations or concatenations directly within the expression.