In Python 3.6 f-strings are introduced.

You can write like this

print (f"So, you're {age} old, {height} tall and {weight} heavy.")

For more information Refer: https://docs.python.org/3/whatsnew/3.6.html

Answer from Nithin R on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-output-formatting
Python - Output Formatting - GeeksforGeeks
... # string modulo operator(%) print("Geeks : -, Portal : %5.2f" % (1, 05.333)) print("Total students : =, Boys : -" % (240, 120)) # print integer value print("%7.3o" % (25)) # print octal value print(".3E" % (356.08977)) # print exponential value
Published   July 11, 2025
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.3 documentation
An optional format specifier can follow the expression. This allows greater control over how the value is formatted. The following example rounds pi to three places after the decimal: >>> import math >>> print(f'The value of pi is approximately {math.pi:.3f}.') The value of pi is approximately 3.142.
🌐
W3Schools
w3schools.com › python › python_string_formatting.asp
Python String Formatting
A modifier is included by adding a colon : followed by a legal formatting type, like .2f which means fixed point number with 2 decimals: ... You can perform Python operations inside the placeholders. ... price = 59 tax = 0.25 txt = f"The price is {price + (price * tax)} dollars" print(txt) Try it Yourself »
🌐
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
Of course it is also possible to format numbers. ... Similar to strings numbers can also be constrained to a specific width. ... Again similar to truncating strings the precision for floating point numbers limits the number of positions after the decimal point. For floating points the padding value represents the length of the complete output. In the example below we want our output to have at least 6 characters with 2 after the decimal point.
🌐
Real Python
realpython.com › python-formatted-output
A Guide to Modern Python String Formatting Tools – Real Python
February 1, 2025 - In this example, you have four replacement fields in the template but only three arguments in the call to .format(). So, you get an IndexError exception. Finally, it’s fine if the arguments outnumber the replacement fields. The excess arguments aren’t used: ... Here, Python ignores the "baz" argument and builds the final string using only "foo" and "bar".
🌐
Mooc
programming-25.mooc.fi › part-4 › 5-print-statement-formatting
Print statement formatting - Python Programming MOOC 2025
The first is the + operator for strings. It allows simple concatenation of string segments: name = "Mark" age = 37 print("Hi " + name + " your age is " + str(age) + " years" ) This method will not work if any of the segments are not strings. In the example above, the variable age has been converted ...
🌐
Python Course
python-course.eu › python-tutorial › formatted-output.php
22. Formatted Output | Python Tutorial | python-course.eu
Additionally, we can modify the formatting with the sign option, which is only valid for number types: ... Enjoying this page? We offer live Python training courses covering the content of this site. ... Just to mention it once more: We could have used empty curly braces in the previous example! Using keyword parameters: print("The capital of {province} is {capital}".format(province="Ontario",capital="Toronto"))
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › python-string-format-python-s-print-format-example
Python String Format – Python S Print Format Example
September 7, 2021 - ... rankings = {"Gonzaga": 31, "Baylor": 28, "Michigan": 25, "Illinois": 24, "Houston": 21} for team, score in rankings.items(): print(f"{team:10} ==> {score:10d}") Template strings are Python strings that use placeholders for the real values.
🌐
Bentley
cissandbox.bentley.edu › sandbox › wp-content › uploads › 2022-02-10-Documentation-on-f-strings-Updated.pdf pdf
Updated 2022 A Guide to Formatting with f-strings in Python
example of using f-strings for debugging purposes which will be covered later. import math · variable = math.pi · Formatting with f-strings Page | 2 · print(f"Using Numeric {variable = }") print(f"|{variable:25}|") print(f"|{variable:<25}|") print(f"|{variable:>25}|") print(f"|{variable:^25}|\n") variable = "Python 3.9" print(f"Using String {variable = }") print(f"|{variable:25}|") print(f"|{variable:<25}|") print(f"|{variable:>25}|") print(f"|{variable:^25}|") The output of this code snippet is: Using Numeric variable = 3.141592653589793 ·
🌐
Python
docs.python.org › fr › 3 › tutorial › inputoutput.html
7. Les entrées/sorties — Documentation Python 3.14.3
Given format % values (where format ... is commonly known as string interpolation. For example: >>> import math >>> print('The value of pi is approximately %5.3f.'...
🌐
TechBeamers
techbeamers.com › python-string-format
Python String Format() - TechBeamers
November 30, 2025 - A float data type also has a couple of variations which you can style using the Python format function. Let’s print a full floating point number. ... Now, let’s print a floating point number truncating after three decimal points. ... Finally, let’s print a floating point number that truncates after three decimal places but does round the final value. ... print('Fixed-point example: <{0:f}>'.format(2.2183)) #Fixed-point example: <2.218300> print('Fixed-point with right alignment example: <{0:25f}>'.format(2.2183)) #Fixed-point with right alignment example: < 2.218300> print('Fixed-point with precision and right alignment example: <{0:<25.10f}>'.format(2.2183)) #Fixed-point with precision and right alignment example: <2.2183000000 >
🌐
Python-Kurs
python-kurs.eu › python3_formatierte_ausgabe.php
Python3-Tutorial: Formatierte Ausgabe
Den mittels der String-Interpolation erzeugten formatierten String, kann man dann z.B. mittels print (also nicht printf) ausgeben. Aber man kann diesen formatierten String auch auf andere Art im Programm weiter verwenden, wie ihn zum Beispiel in eine Datenbank übertragen. Seit Python 2.6 eingeführt worden ist, wird jedoch von der Python-Gemeinde empfohlen die String-Methode "format" stattdessen zu verwenden.
🌐
mkaz.blog
mkaz.blog › code › python-string-format-cookbook
String Formatting - mkaz.blog
July 15, 2021 - name = "Python" version = 3.11 # F-strings (recommended) message = f"Welcome to {name} {version}!" # .format() method message = "Welcome to {} {}!".format(name, version) # % formatting (legacy) message = "Welcome to %s %s!" % (name, version) # All output: Welcome to Python 3.11! Performance comparison (Python 3.11): import timeit name = "World" # F-string: ~0.05 microseconds t = timeit.timeit(lambda: f"Hello {name}!", number=1000000) print("Time: {t:.6f} seconds") # .format(): ~0.15 microseconds t = timeit.timeit(lambda: "Hello {}!".format(name), number=1000000) print("Time: {t:.6f} seconds")
🌐
JournalDev
journaldev.com › 17370 › python-print-format
Python print() | DigitalOcean
August 4, 2022 - So, as many item you want to print, just put them together as arguments. If see the example of the previous section, you will notice that that variables are separated with a space. But you can customize it to your own style. Suppose in the previous code, you want to separate the values using underscore(_). Then you should pass underscore as the value of sep keyword. The following function will illustrate you the idea of using python print sep keyword.
🌐
Python
docs.python.org › 3.1 › tutorial › inputoutput.html
7. Input and Output — Python v3.1.5 documentation
September 4, 2012 - It interprets the left argument much like a sprintf()-style format string to be applied to the right argument, and returns the string resulting from this formatting operation. For example: >>> import math >>> print('The value of PI is approximately %5.3f.' % math.pi) The value of PI is ...
🌐
Python
docs.python.org › 3.4 › library › string.html
Python string formatting
String of ASCII characters which are considered printable. This is a combination of digits, ascii_letters, punctuation, and whitespace. ... A string containing all ASCII characters that are considered whitespace. This includes the characters space, tab, linefeed, return, formfeed, and vertical tab.
Top answer
1 of 16
1114

The WHY: dates are objects

In Python, dates are objects. Therefore, when you manipulate them, you manipulate objects, not strings or timestamps.

Any object in Python has TWO string representations:

  • The regular representation that is used by print can be get using the str() function. It is most of the time the most common human readable format and is used to ease display. So str(datetime.datetime(2008, 11, 22, 19, 53, 42)) gives you '2008-11-22 19:53:42'.

  • The alternative representation that is used to represent the object nature (as a data). It can be get using the repr() function and is handy to know what kind of data your manipulating while you are developing or debugging. repr(datetime.datetime(2008, 11, 22, 19, 53, 42)) gives you 'datetime.datetime(2008, 11, 22, 19, 53, 42)'.

What happened is that when you have printed the date using print, it used str() so you could see a nice date string. But when you have printed mylist, you have printed a list of objects and Python tried to represent the set of data, using repr().

The How: what do you want to do with that?

Well, when you manipulate dates, keep using the date objects all long the way. They got thousand of useful methods and most of the Python API expect dates to be objects.

When you want to display them, just use str(). In Python, the good practice is to explicitly cast everything. So just when it's time to print, get a string representation of your date using str(date).

One last thing. When you tried to print the dates, you printed mylist. If you want to print a date, you must print the date objects, not their container (the list).

E.G, you want to print all the date in a list :

for date in mylist :
    print str(date)

Note that in that specific case, you can even omit str() because print will use it for you. But it should not become a habit :-)

Practical case, using your code

import datetime
mylist = []
today = datetime.date.today()
mylist.append(today)
print mylist[0] # print the date object, not the container ;-)
2008-11-22

# It's better to always use str() because :

print "This is a new day : ", mylist[0] # will work
>>> This is a new day : 2008-11-22

print "This is a new day : " + mylist[0] # will crash
>>> cannot concatenate 'str' and 'datetime.date' objects

print "This is a new day : " + str(mylist[0]) 
>>> This is a new day : 2008-11-22

Advanced date formatting

Dates have a default representation, but you may want to print them in a specific format. In that case, you can get a custom string representation using the strftime() method.

strftime() expects a string pattern explaining how you want to format your date.

E.G :

print today.strftime('We are the %d, %b %Y')
>>> 'We are the 22, Nov 2008'

All the letter after a "%" represent a format for something:

  • %d is the day number (2 digits, prefixed with leading zero's if necessary)
  • %m is the month number (2 digits, prefixed with leading zero's if necessary)
  • %b is the month abbreviation (3 letters)
  • %B is the month name in full (letters)
  • %y is the year number abbreviated (last 2 digits)
  • %Y is the year number full (4 digits)

etc.

Have a look at the official documentation, or McCutchen's quick reference you can't know them all.

Since PEP3101, every object can have its own format used automatically by the method format of any string. In the case of the datetime, the format is the same used in strftime. So you can do the same as above like this:

print "We are the {:%d, %b %Y}".format(today)
>>> 'We are the 22, Nov 2008'

The advantage of this form is that you can also convert other objects at the same time.
With the introduction of Formatted string literals (since Python 3.6, 2016-12-23) this can be written as

import datetime
f"{datetime.datetime.now():%Y-%m-%d}"
>>> '2017-06-15'

Localization

Dates can automatically adapt to the local language and culture if you use them the right way, but it's a bit complicated. Maybe for another question on SO(Stack Overflow) ;-)

2 of 16
431
import datetime
print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")

Edit:

After Cees' suggestion, I have started using time as well:

import time
print time.strftime("%Y-%m-%d %H:%M")
🌐
Python
wiki.python.org › moin › SimplePrograms
SimplePrograms
What is your name?\n') number = random.randint(1, 20) print ('Well, {0}, I am thinking of a number between 1 and 20.'.format(name)) while guesses_made < 6: guess = int(input('Take a guess: ')) guesses_made += 1 if guess < number: print ('Your guess is too low.') if guess > number: print ('Your ...
🌐
Codegrepper
codegrepper.com › code-examples › python › python+print+format
python print format Code Example
for x in numbers: print "{:10.4f}".format(x) prints 23.2300 0.1233 1.0000 4.2230 9887.2000