Yes, but you have to pass them in as arguments to format, and then refer to them wrapped in {} like you would the argument name itself:

print('\n{:^{display_width}}'.format('some text here', display_width=display_width))

Or shorter but a little less explicit:

print('\n{:^{}}'.format('some text here', display_width))

Since this question was originally posted, Python 3.6 has added f-strings, which allow you to do this without using the format method and it uses variables which are in scope rather than having to pass in the named variables as keyword arguments:

display_width = 50
text = 'some text here'
print(f'\n{text:^{display_width}}')
Answer from Brian Campbell on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_strings_format.asp
Python - Format Strings
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... age = 36 #This will produce an error: txt = "My name is John, I am " + age print(txt) Try it Yourself » · But we can combine strings and numbers by using f-strings or the format() method!
People also ask

How does the str.format() method work in Python?
The str.format() method allows you to create formatted strings with placeholders enclosed in curly braces {}. You replace these placeholders with values using the format() method.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › format in python
Format in Python: Comprehensive Guide to String Formatting - upGrad
What are f-strings, and how do they simplify string formatting in Python?
F-strings are a feature introduced in Python 3.6 that allows you to embed expressions inside string literals using {} placeholders. They simplify string formatting by providing a concise and readable way to insert values into strings.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › format in python
Format in Python: Comprehensive Guide to String Formatting - upGrad
How can I format strings with both positional and keyword arguments using str.format()?
You can mix positional and keyword arguments when using str.format() by providing values in both ways. Positional arguments are replaced in the order of appearance, while keyword arguments are replaced based on their names.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › format in python
Format in Python: Comprehensive Guide to String Formatting - upGrad
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-formatting-in-python
String Formatting in Python - GeeksforGeeks
In this code, the string 'The value of pi is: %5.4f' contains a format specifier %5.4f. The %5.4f format specifier is used to format a floating-point number with a minimum width of 5 and a precision of 4 decimal places. ... In the given code, the formatting string Python is converted to Integer and floating point with %d,%f.
Published   January 14, 2026
🌐
Real Python
realpython.com › python-formatted-output
A Guide to Modern Python String Formatting Tools – Real Python
February 1, 2025 - You can use variables in Python’s .format() method by placing them inside curly braces and passing them as arguments. Format specifiers in Python control how values appear when formatted, using components like fill, align, sign, width, and type.
🌐
Python
peps.python.org › pep-3101
PEP 3101 – Advanced String Formatting | peps.python.org
If an object does not define its own format specifiers, a standard set of format specifiers is used. These are similar in concept to the format specifiers used by the existing ‘%’ operator, however there are also a number of differences. The general form of a standard format specifier is:
🌐
Python
docs.python.org › 3 › library › string.html
Common string operations — Python 3.14.3 documentation
Because arg_name is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings '10' or ':-]') within a format string. The arg_name can be followed by any number of index or attribute expressions. An expression of the form '.name' selects the named attribute using getattr(), while an expression of the form '[index]' does an index lookup using __getitem__(). Changed in version 3.1: The positional argument specifiers can be omitted for str.format(), so '{} {}'.format(a, b) is equivalent to '{0} {1}'.format(a, b).
🌐
My Docs
codebay.ai › welcome to codebay › glossaries › data types › string › format specifier
Format Specifier | Codebay
April 7, 2024 - A format specifier in Python provides a mechanism to control the exact display of values as strings. It enables the programmer to define the type of data and its presentation style.
Find elsewhere
🌐
Learn Python
learnpython.org › en › String_Formatting
String Formatting - Learn Python - Free Interactive Python Tutorial
Here are some basic argument specifiers you should know: %s - String (or any object with a string representation, like numbers) ... %.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › format in python
Format in Python: Comprehensive Guide to String Formatting - upGrad
October 16, 2024 - In this example, we use format specifiers (:d for integers and :.2f for floating-point numbers) to control the formatting of the values. In Python, you can use the %s format specifier to convert values to strings using the format function in Python before formatting.
🌐
The Python Coding Stack
thepythoncodingstack.com › p › custom-f-string-format-specifiers-python
I Want My Own Fancy F-String Format Specifiers… Sure You Can
March 19, 2025 - If the format specifier is the empty string, which is falsy, then .__format__() returns the str() representation: ... Now, back to ClubMember. Do you want to join a forum to discuss Python further with other Pythonistas? Upgrade to a paid subscription here on The Python Coding Stack to get exclusive access to The Python Coding Place's members' forum.
🌐
LabEx
labex.io › questions › how-to-use-format-specifiers-in-python-91
How to Use Format Specifiers in Python | LabEx
July 25, 2024 - Format specifiers in Python are denoted by the {} curly braces, and they are used to insert values into a string. These values can be variables, expressions, or even other formatting instructions.
🌐
W3Schools
w3schools.com › python › ref_string_format.asp
Python String format() Method
Python Examples Python Compiler ... Python Certificate Python Training ... The format() method formats the specified value(s) and insert them inside the string's placeholder....
🌐
Codesolid
codesolid.com › python-format-strings
Python Format Strings: Beginner to Expert — CodeSolid.com 0.1 documentation
The f-string is a bit harder to read. Still, it’s simply the expression, a colon to indicate a format specifier is about to show up, and the specifier itself: .2f, which in effect tells Python, “format it as a fixed-point number with two digits of decimal precision, please.”
🌐
Python Morsels
pythonmorsels.com › string-formatting
Python f-string tips & cheat sheets - Python Morsels
April 12, 2022 - It's nifty that we can customize the format of strings and numbers within an f-string. But what about other types? What else supports format specifiers? Python's datetime.datetime class (along with datetime.date and datetime.time) supports string formatting which uses the same syntax as the strftime method on these objects.
🌐
GeeksforGeeks
geeksforgeeks.org › string-formatting-in-python
Python String Formatting - How to format String? - GeeksforGeeks
In this code, the string 'The value of pi is: %5.4f' contains a format specifier %5.4f. The %5.4f format specifier is used to format a floating-point number with a minimum width of 5 and a precision of 4 decimal places. ... In the given code, the formatting string Python is converted to Integer and floating point with %d,%f.
Published   August 21, 2024
🌐
CodeRivers
coderivers.org › blog › python-format-specifier
Python Format Specifier: A Comprehensive Guide - CodeRivers
April 5, 2025 - Format specifiers in Python are special codes used within strings to define how a value should be presented.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › str › formatting.html
% (String Formatting Operator) — Python Reference (The Right Way) 0.1 documentation
String (converts any Python object using str()). If the object or format provided is a unicode string, the resulting string will also be unicode. The precision determines the maximal number of characters used. ... No argument is converted, results in a ‘%’ character in the result. If format specifier is a Unicode object, or if any of the objects being converted using the %s conversion are Unicode objects, the result will also be a Unicode object.
🌐
LabEx
labex.io › tutorials › python-how-to-use-format-specifiers-in-python-print-397698
How to use format specifiers in Python print | LabEx
Learn how to use format specifiers in Python's print function to create dynamic and customized output. Explore advanced formatting techniques for better data representation.
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 3-2-formatted-strings
3.2 Formatted strings - Introduction to Python Programming | OpenStax
March 13, 2024 - In an f-string, a replacement field may include a format specifier introduced by a colon. A format specifier defines how a value should be formatted for display.