In Python2, print was a keyword which introduced a statement:

print "Hi"

In Python3, print is a function which may be invoked:

print ("Hi")

In both versions, % is an operator which requires a string on the left-hand side and a value or a tuple of values or a mapping object (like dict) on the right-hand side.

So, your line ought to look like this:

print("a=%d,b=%d" % (f(x,n),g(x,n)))

Also, the recommendation for Python3 and newer is to use {}-style formatting instead of %-style formatting:

print('a={:d}, b={:d}'.format(f(x,n),g(x,n)))

Python 3.6 introduces yet another string-formatting paradigm: f-strings.

print(f'a={f(x,n):d}, b={g(x,n):d}')
Answer from Robᵩ on Stack Overflow
Discussions

c - What is the conversion specifier for printf that formats a long? - Stack Overflow
The printf function takes an argument type, such as %d or %i for a signed int. However, I don't see anything for a long value. More on stackoverflow.com
🌐 stackoverflow.com
python - What is print(f"...") - Stack Overflow
I am reading through a python script that takes an input of XML files and outputs an XML file. However, I do not understand the printing syntax. Can someone please explain what f in print(f"..... More on stackoverflow.com
🌐 stackoverflow.com
how to do printf with unsigned short long data type?
Loading · ×Sorry to interrupt · Refresh More on forum.microchip.com
🌐 forum.microchip.com
string - Wrap long lines in Python - Stack Overflow
As of Python 3.9, a DeprecationWarning is raised: ... The replacements are very readable. In particular, this approach makes writing code that generates code or mathematical formulas a very pleasant task. Rarely, the method str.format may be suitable, due to what substitutions are needed. It can be used as follows: print(( 'This message is so long ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-output-formatting
Python - Output Formatting - GeeksforGeeks
In Python, output formatting refers to the way data is presented when printed or logged. Proper formatting makes information more understandable and actionable.
Published   July 11, 2025
🌐
Python Course
python-course.eu › python-tutorial › formatted-output.php
22. Formatted Output | Python Tutorial | python-course.eu
Is there a printf in Python? A burning question for Python newbies coming from C, Perl, Bash or other programming languages who have this statement or function. To answer "Python has a print function and no printf function" is only one side of the coin or half of the truth.
🌐
Yale
neuron.yale.edu › neuron › static › py_doc › programming › io › printf.html
Printf (Formatted Output) — NEURON 7.7 documentation
For code written in Python, it is generally more practical to use Python string formatting and file IO. ... h.printf places output on the standard output. h.fprint places output on the file opened with the h.wopen(filename) command (standard output if no file is opened).
🌐
W3Schools
w3schools.com › c › c_data_types_extended.php
C More Data Types (Extended Types)
May 17, 2026 - int normalInt = 1000; // standard int double normalDouble = 3.14; // standard double short int small = -100; // smaller int unsigned int count = 25; // only positive int long int big = 1234567890; // larger int long long int veryBig = 9223372036854775807; // very large int unsigned long long int huge = 18446744073709551615U; // very large, only positive long double precise = 3.141592653589793238L; // extended precision printf("Normal int: %d\n", normalInt); printf("Normal double: %lf\n", normalDouble); printf("Small: %hd\n", small); printf("Count: %u\n", count); printf("Big: %ld\n", big); printf("Very Big: %lld\n", veryBig); printf("Huge: %llu\n", huge); printf("Precise: %Lf\n", precise);
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use print() in Python | note.nkmk.me
May 10, 2023 - Unless you're familiar with the C-style printf, which uses conversion specifiers like %d, %f.2, and %s, it's recommended to use the format() method or f-strings, as mentioned in the official documentation. Note that f-strings were added in Python 3.6 and are not available in earlier versions.
Top answer
1 of 7
150

The f means Formatted string literals and it's new in Python 3.6.


A formatted string literal or f-string is a string literal that is prefixed with f or F. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.


Some examples of formatted string literals:

>>> name = "Fred"
>>> f"He said his name is {name}."
"He said his name is Fred."

>>> name = "Fred"
>>> f"He said his name is {name!r}."
"He said his name is Fred."

>>> f"He said his name is {repr(name)}." # repr() is equivalent to !r
"He said his name is Fred."

>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}" # nested fields
result: 12.35

>>> today = datetime(year=2023, month=1, day=27)
>>> f"{today:%B %d, %Y}" # using date format specifier
January 27, 2023

>>> number = 1024
>>> f"{number:#0x}" # using integer format specifier
0x400
2 of 7
63

In Python 3.6, the f-string, formatted string literal, was introduced(PEP 498). In short, it is a way to format your string that is more readable and fast.

Example:

agent_name = 'James Bond'
kill_count = 9


# old ways
print("%s has killed %d enemies" % (agent_name,kill_count))

print('{} has killed {} enemies'.format(agent_name,kill_count))
print('{name} has killed {kill} enemies'.format(name=agent_name,kill=kill_count))
    

# f-strings way
print(f'{agent_name} has killed {kill_count} enemies')

The f or F in front of strings tell Python to look at the values , expressions or instance inside {} and substitute them with the variables values or results if exists. The best thing about f-formatting is that you can do cool stuff in {}, e.g. {kill_count * 100}.

You can use it to debug using print e.g.

print(f'the {agent_name=}.')
# the agent_name='James Bond'

Formatting, such as zero-padding, float and percentage rounding is made easier:

print(f'{agent_name} shoot with {9/11 : .2f} or {9/11: .1%} accuracy')
# James Bond shoot with  0.82 or  81.8% accuracy 

Even cooler is the ability to nest and format. Example date


from datetime import datetime

lookup = {
    '1': 'st',
    '21': 'st',
    '31': 'st',
    '2': 'nd',
    '22': 'nd',
    '3': 'rd',
    '23': 'rd'
}

dato = datetime.now()

print(f"{dato: %B %-d{lookup.get(f'{dato:%-d}', 'th')} %Y}")

# April 23rd 2022

Pretty formatting is also easier

tax = 1234

print(f'{tax:,}') # separate 1k \w comma
# 1,234

print(f'{tax:,.2f}') # all two decimals 
# 1,234.00

print(f'{tax:~>8}') # pad left with ~ to fill eight characters or < other direction
# ~~~~1234

print(f'{tax:~^20}') # centre and pad
# ~~~~~~~~1234~~~~~~~~

The __format__ allows you to funk with this feature. Example


class Money:
    
    def __init__(self, value, currency='€'):
        self.currency = currency
        self.value = value
        
    def __repr__(self):
        return f'Money(value={self.value}, currency={self.currency})'
        
    def __format__(self, *_):
        
        return f"{self.currency}{float(self.value):.2f}"
        
        
tax = 12.3446
money = Money(tax, currency='$')

print(f'{money}')
# $12.34

print(money)
# Money(value=12.3446, currency=$)

There is much more. Readings:

  • PEP 498 Literal String Interpolation
  • Python String Formatting
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-long
C Long - GeeksforGeeks
June 15, 2026 - #include <stdio.h> int main() { // Max 32-bit int int a = 2147483647; // Exceeds standard 32-bit int long b = 3000000000L; printf("Long Value: %ld\n", b); printf("Sizes: int = %zu, long = %zu bytes\n", sizeof(a), sizeof(b)); return 0; } Output ...
🌐
Quora
quora.com › Why-does-printing-an-“long-long-integer”-with-format-specifier-“-d”-give-result-as-zero-even-if-they-are-both-integers
Why does printing an “long long integer” with format specifier “%d” give result as zero, even if they are both integers? - Quora
Answer (1 of 2): The variables i, sum and product are declared as long long int and format specifier for it is ‘%lld’. I’m not sure, maybe it’ll solve problem. Edit: This can be a reason: Size of integer is 4 Bytes while that of long long int is 8 Bytes (On a 64-bit machine).
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.6 documentation
There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use. This chapter will discuss some of the possibilities. Fa...
🌐
W3Schools
w3schools.com › c › ref_stdio_printf.php
C stdio printf() Function
The printf() function writes a formatted string to the console.
🌐
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
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.
Top answer
1 of 6
321
def fun():
    print(('{0} Here is a really long '
           'sentence with {1}').format(3, 5))

Adjacent string literals are concatenated at compile time, just as in C. 2.4.2. String literal concatenation is a good place to start for more information.

2 of 6
87

Using concatenation of adjacent string literals, together with formatted string literals is the way to go:

x = 2
sep = 2 * '\n'
print(
    'This message is so long that it requires '
    f'more than {x} lines.{sep}'
    'And more lines may be needed.')

This approach complies with PEP 8 and allows better use of space.

No + operators needed, no backslashes for line continuation, no irregularities of indentation, no error-prone += to an accumulator string variable (which can be mistyped as =, resulting in a silent error), no stray parenthesis hanging below the print (the arguments are placed in their own level of indentation, and the next element at the indentation level of print is the next statement).

Starting the strings on the line below the line that contains the print( reduces indentation, and is more readable. Readability stems from both print( standing out, by being on its own line, and by the uniform alignment of consecutive statements of this form.

The reduction in indentation from this approach becomes more evident when raising exceptions:

raise ModuleNotFoundError(
    'aaaaaaaaaaaaaaaaaaaaaaaa'
    'aaaaaaaaaaaaaaaaaaaaaaaa'
    f'aaaaa {x} aaaaa')

Regarding formatted string literals (signified by the prefix "f", as in f'...'), raw strings can be formatted string literals, by combining the prefixes "r" and "f":

rf'This is a formatted raw string, {sep}here is a backslash \.'

Note that raw strings are necessary for including literal backslashes without writing \\. Otherwise, in a future CPython version, a SyntaxError will be raised. As of Python 3.9, a DeprecationWarning is raised:

python -X dev -c '"\q"'

outputs:

<string>:1: DeprecationWarning: invalid escape sequence \q

The replacements are very readable. In particular, this approach makes writing code that generates code or mathematical formulas a very pleasant task.

Rarely, the method str.format may be suitable, due to what substitutions are needed. It can be used as follows:

print((
    'This message is so long that it requires '
    'more than {x} lines.{sep}'
    'And more lines may be needed.'
    ).format(x=x, sep=sep))
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › hardware and peripherals › raspberry pi pico › sdk
Bug in printf? [not a bug in printf] - Raspberry Pi Forums
June 24, 2021 - { uint32_t u32 = 1000; uint64_t u64 = 2000; int32_t i32 = 3000; int64_t i64 = 4000; printf("u32 %u u64 %lu i32 %d i64 %ld\n", u32, u64, i32, i64); } produces: u32 1000 u64 2000 i32 0 i64 3000 That's because your printf specifiers don't match with what you are passing. %ld says to print a long but you are passing a long long which would be %lld If you #include <inttypes.h> then you can use the standard's macros to get the correct specifiers.
🌐
Wyzant
wyzant.com › resources › ask an expert
How do you format an unsigned long long int using printf? | Wyzant Ask An Expert
May 3, 2019 - A normal number is 0.I assume this unexpected result is from printing the `unsigned long long int`. How do you `printf()` an `unsigned long long int`?
🌐
U-Boot Documentation
docs.u-boot.org › en › v2024.10 › develop › printf.html
Printf() format codes — Das U-Boot unknown version documentation
Printf() format codes · View page source · Each conversion specification consists of: leading ‘%’ character · zero or more flags · an optional minimum field width · an optional precision field preceded by ‘.’ · an optional length modifier · a conversion specifier ·