How to convert an integer to a string in Python

Python has a built-in function str() which converts the passed argument into a string format.

1 of 3

The str() function returns a string version of an object. The object can be int, char, or a string. If the object is not passed as an argument, then it returns an empty string.

Syntax

str(object, encoding=encoding, errors=errors)
svg viewer

Example

The following code converts an integer to a string and appends it to the other string.

# An integer version of year
integer_year = 2019
# A string version of year
string_year = str(2019)
# Concatinates the string with the string version of year
print("This is " + string_year + ".")
# Returns none if no argument is provided
print(str())

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved