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 Top answer 1 of 5
131
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'
2 of 5
44
You can fetch the padding value from the argument list:
print '%*s : %*s' % (20, "Python", 20, "Very Good")
You can even insert the padding values dynamically:
width = 20
args = ("Python", "Very Good")
padded_args = zip([width] * len(args), args)
# Flatten the padded argument list.
print "%*s : %*s" % tuple([item for list in padded_args for item in list])
Videos
08:20
How to Use f-Strings (Formatted Strings) in Python - YouTube
19:43
Every F-String Trick In Python Explained - YouTube
Python String Formatting: Your Ultimate Guide to Clean and ...
00:43
Easy Python String Formatting with f-strings - YouTube
Python f-Strings - Advanced String Formatting tutorial for beginners ...
06:12
The Python String .format() Method - YouTube
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
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.
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 ...
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.
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.