How to convert int to string in Python

Numerous techniques exist in Python for converting a number to a string. Using the built-in str() function, which transforms numerical values-including integers-to their string representations, is one popular method. To retrieve the matching string, just supply the integer variable as an input to str(). An alternative approach is to use string formatting techniques like as f-strings, in which the integer variable is automatically converted to a string by enclosing it in curly brackets {} within a string literal. To complete the conversion, you may also utilize the %s formatting specifier within strings or the. format() function.

Syntax -

Let's understand the following example.

Example - 1 Using the str() function

Output:

<class 'int'>
25
<class 'str'>
25

Explanation:

An integer variable called n is initialized with the value 25 in this passage of Python code. Next, it confirms that n is an integer by printing its type and value. Next, it uses the str() method to turn n into a string, which it then assigns to con_num. Subsequently, the type and value of con_num are printed, verifying that it is a string with the same value as n, which is "25". The result shows how to convert an integer to a string while maintaining the original value. It also illustrates how to change the type from int to str.

Example - 2 Using the "%s" integer

Output:

<class 'int'>
<class 'str'>

Explanation:

An integer variable called n is initialized with the value 10 in this Python example. The software first outputs its type, verifying that it is an integer. It then uses the %s format specifier in a formatted string expression to turn n into a string, which it then assigns to con_n. Following the conversion, it outputs con_n's type and confirms that it is a string. This conversion technique turns the integer value n into a string representation by using %s as a placeholder for the value. This enables for string formatting.

Example - 3: Using the .format() function

Output:

<class 'int'>
<class 'str'>

Explanation:

An integer variable called n is initialized with the value 10 in this Python example. The software first outputs n's type, verifying that it is an integer. Next, it uses a string expression and the. format() method to convert n to a string, which it then assigns to con_n. After the conversion, it confirms that con_n is a string by printing its type. Python's.format() function is a flexible way to format strings; it lets you dynamically insert variables into strings without changing their original data types.

Example - 4: Using f-string

Output:

<class 'int'>
<class 'str'>

Explanation:

An integer variable called n is initialized with the value 10 in this Python example. The software first outputs n's type, verifying that it is an integer. Next, it assigns n to conv_n and encloses it in curly brackets {} to transform it into a string using f-string formatting. Following the conversion, it confirms that the object is a string by printing the type of conv_n. Variables and expressions may be directly placed into string literals using F-strings, which provide a clear and understandable method of formatting strings in Python while preserving their original data types.

Every technique for changing the integer data type to the string data type has been specified. You can use whatever one best suits your needs.






Latest Courses