Starting with Python 3.6, formatting in Python can be done using formatted string literals or f-strings:

hours, minutes, seconds = 6, 56, 33
f'{hours:02}:{minutes:02}:{seconds:02} {"pm" if hours > 12 else "am"}'

or the str.format function starting with 2.7:

"{:02}:{:02}:{:02} {}".format(hours, minutes, seconds, "pm" if hours > 12 else "am")

or the string formatting % operator for even older versions of Python, but see the note in the docs:

"%02d:%02d:%02d" % (hours, minutes, seconds)

And for your specific case of formatting time, thereโ€™s time.strftime:

import time

t = (0, 0, 0, hours, minutes, seconds, 0, 0, 0)
time.strftime('%I:%M:%S %p', t)
Answer from Konrad Rudolph on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-integer-to-string-in-python
Convert integer to string in Python - GeeksforGeeks
str() function is the simplest and most commonly used method to convert an integer to a string. ... Explanation: str(n) converts n to a string, resulting in '42'. For Python 3.6 or later, f-strings provide a quick way to format and convert values.
Published ย  July 12, 2025
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to convert an int to a string in python
How to convert an int to a string in Python
To fix this, you must manually cast the integer to a string using the str() function before concatenation. Alternatively, you can avoid this error entirely by using f-strings or the .format() method.
Top answer
1 of 9
158

Starting with Python 3.6, formatting in Python can be done using formatted string literals or f-strings:

hours, minutes, seconds = 6, 56, 33
f'{hours:02}:{minutes:02}:{seconds:02} {"pm" if hours > 12 else "am"}'

or the str.format function starting with 2.7:

"{:02}:{:02}:{:02} {}".format(hours, minutes, seconds, "pm" if hours > 12 else "am")

or the string formatting % operator for even older versions of Python, but see the note in the docs:

"%02d:%02d:%02d" % (hours, minutes, seconds)

And for your specific case of formatting time, thereโ€™s time.strftime:

import time

t = (0, 0, 0, hours, minutes, seconds, 0, 0, 0)
time.strftime('%I:%M:%S %p', t)
2 of 9
103

The OP & accepted answer focus on formatting time, but the OP question itself discusses formatting numbers to strings in Python. In many cases, the output requires additional data fields be included along with timestamps, all of which include formatting numbers as strings.

Below are a variety of non time-based examples of formatting numbers as strings, and different ways to do so, starting with the existing string format operator (%) which has been around for as long as Python has been around (meaning this solution is compatible across Python 1.x, 2.x, and 3.x):

>>> "Name: %s, age: %d" % ('John', 35) 
'Name: John, age: 35' 
>>> i = 45 
>>> 'dec: %d/oct: %#o/hex: %#X' % (i, i, i) 
'dec: 45/oct: 055/hex: 0X2D' 
>>> "MM/DD/YY = %02d/%02d/%02d" % (12, 7, 41) 
'MM/DD/YY = 12/07/41' 
>>> 'Total with tax: $%.2f' % (13.00 * 1.0825) 
'Total with tax: $14.07' 
>>> d = {'web': 'user', 'page': 42} 
>>> 'http://xxx.yyy.zzz/%(web)s/%(page)d.html' % d 
'http://xxx.yyy.zzz/user/42.html' 

Starting in Python 2.6 (meaning it works for 2.x and 3.x), there is an alternative: the str.format() method. Here are the equivalent snippets to the above but using str.format():

>>> "Name: {0}, age: {1}".format('John', 35) 
'Name: John, age: 35' 
>>> i = 45 
>>> 'dec: {0}/oct: {0:#o}/hex: {0:#X}'.format(i) 
'dec: 45/oct: 0o55/hex: 0X2D' 
>>> "MM/DD/YY = {0:02d}/{1:02d}/{2:02d}".format(12, 7, 41) 
'MM/DD/YY = 12/07/41' 
>>> 'Total with tax: ${0:.2f}'.format(13.00 * 1.0825) 
'Total with tax: $14.07' 
>>> d = {'web': 'user', 'page': 42} 
>>> 'http://xxx.yyy.zzz/{web}/{page}.html'.format(**d) 
'http://xxx.yyy.zzz/user/42.html'

Like Python 2.6+, all Python 3 releases (so far) understand how to do both. I shamelessly ripped this stuff straight out of my hardcore Python intro book and the slides for the Intro+Intermediate Python courses I offer from time-to-time. :-)

Aug 2018 UPDATE: Of course, now that we have the f-string feature introduced in 3.6 (only works in 3.6 and newer), we need the equivalent examples of that; yes, another alternative:

>>> name, age = 'John', 35
>>> f'Name: {name}, age: {age}'
'Name: John, age: 35'

>>> i = 45
>>> f'dec: {i}/oct: {i:#o}/hex: {i:#X}'
'dec: 45/oct: 0o55/hex: 0X2D'

>>> m, d, y = 12, 7, 41
>>> f"MM/DD/YY = {m:02d}/{d:02d}/{y:02d}"
'MM/DD/YY = 12/07/41'

>>> f'Total with tax: ${13.00 * 1.0825:.2f}'
'Total with tax: $14.07'

>>> d = {'web': 'user', 'page': 42}
>>> f"http://xxx.yyy.zzz/{d['web']}/{d['page']}.html"
'http://xxx.yyy.zzz/user/42.html'
๐ŸŒ
Python Principles
pythonprinciples.com โ€บ blog โ€บ converting-integer-to-string-in-python
Converting integer to string in Python โ€“ Python Principles
To convert an integer to a string, use the str() built-in function. The function takes an int as input and produces a string as its output. Here are some examples.
๐ŸŒ
Cmlabs
cmlabs.co โ€บ home โ€บ references โ€บ web-development โ€บ how-to-convert-int-to-string
How to Convert Int to String in Phyton with Examples
June 18, 2024 - It then turns n into a string by using a string expression and the .format() function, which it assigns to con_n. Following the conversion, it prints the type of con_n to verify that it is a string.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ how to convert int to string in python
How to Convert int to string in Python - Scaler Topics
May 12, 2024 - ... The syntax for using the %s ... .format() function in int to string conversion is using it with an empty string and then writing the integer value inside parenthesis....
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ int-to-string-python
4 Ways to Convert Int to String in Python | FavTutor
September 14, 2021 - str() is a built-in function in python programming used to convert the integer into a string. Apart from integer conversion, the str() function can take any python data type as a parameter and convert it into the string format.
Find elsewhere
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ convert int to string in python (6 methods with examples)
Convert Int To String In Python (6 Methods With Examples)
April 11, 2024 - One common approach to convert int to string in Python is to use built-in functions like str(), which directly converts an integer to its string representation. For instance, str(123) returns the string '123'.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ convert integer to string in python
Convert Integer to String in Python - Spark By {Examples}
May 21, 2024 - We can use f-strings to convert an integer to a string by including the integer as part of the f-string. # F-String method num = 342 str_num = f"{num}" print(str_num) # Output: # "342" We can also specify a format specifier inside the curly ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-convert-an-integer-to-a-string-in-python-397677
How to convert an integer to a string in Python | LabEx
Python 3.6 introduced a new way to convert integers to strings using formatted string literals, also known as f-strings.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Format Strings and Numbers in Python: format() | note.nkmk.me
May 18, 2023 - Built-in Functions - format() โ€” Python 3.11.3 documentation ยท This function takes the original string (str) and number (int or float) to be formatted as its first argument, and the format specification string as its second argument.
๐ŸŒ
Real Python
realpython.com โ€บ convert-python-string-to-int
How to Convert a Python String to int โ€“ Real Python
January 16, 2021 - In Python, you can convert a Python int to a string using str(): ... In this example, str() is smart enough to interpret the binary literal and convert it to a decimal string. If you want a string to represent an integer in another number system, ...
๐ŸŒ
W3Schools
w3schools.in โ€บ python โ€บ examples โ€บ convert-int-to-string
Python Program to Convert Int to String
Using f-strings feature (available in Python 3.6 and above). Using % operator (also known as the "string formatting operator"). To convert an integer to a string in Python, the str() function can be used. This function takes an integer as an argument and returns the corresponding string ...
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python string to int() and int to string tutorial: type conversion in python
Python String to Int() and Int to String Tutorial: Type Conversion in Python
December 1, 2023 - How to Convert Python Int to String: To convert an integer to string in Python, use the str() function. This function takes any data type and converts it into a string, including integers.
๐ŸŒ
SheCodes
shecodes.io โ€บ athena โ€บ 2142-converting-an-integer-to-string-in-python
[Python] - Converting an Integer to String in Python - | SheCodes
Learn how to convert an integer to a string in Python by using the `str()` function or casting the integer as a string.