w3resource

Python: Convert integer to string

Python Basic - 1: Exercise-144 with Solution

Write a Python program to convert integer to string.

Sample Solution-1:

Python Code:

# Defining a string variable 'language' with the value "Python".
language = "Python"

# Defining a floating-point variable 'version' with the value 3.6.
version = 3.6

# Printing a concatenated string with information about the programming language and its version.
print('Language: ' + language + ',' + ' Version: ' + str(version))

Sample Input:
language = "Python"
version = 3.6

Sample Output:

Language: Python, Version: 3.6

Explanation:

Here is a breakdown of the above Python code:

  • Variable Definition:
    • language = "Python": Assigns the string value "Python" to the variable 'language'.
  • Variable Definition:
    • version = 3.6: Assigns the floating-point value 3.6 to the variable 'version'.
  • Print Statement:
    • print('Language: ' + language + ',' + ' Version: ' + str(version)): Prints a concatenated string containing information about the programming language and its version. The str(version) is used to convert the floating-point 'version' variable to a string for concatenation.

Sample Solution-2:

Python Code:

# Defining a string variable 'language' with the value "Python".
language = "Python"

# Defining a floating-point variable 'version' with the value 3.6.
version = 3.6

# Converting the 'version' variable to a string using the '{}'.format() method.
str_version = '{}'.format(version)

# Printing the concatenated string with information about the programming language and its version.
print('Language: ' + language + ',' + ' Version: ' + str_version)

Sample Input:
language = "Python"
version = 3.6

Sample Output:

Language: Python, Version: 3.6

Explanation:

Here is a breakdown of the above Python code:

  • Variable Definition:
    • language = "Python": Assigns the string value "Python" to the variable 'language'.
  • Variable Definition:
    • version = 3.6: Assigns the floating-point value 3.6 to the variable 'version'.
  • String Formatting:
    • str_version = '{}'.format(version): Converts the floating-point 'version' variable to a string using the format() method.
  • Print Statement:
    • print('Language: ' + language + ',' + ' Version: ' + str_version): Prints a concatenated string containing information about the programming language and its version.

Sample Solution-3:

Python Code:

# Defining a string variable 'language' with the value "Python".
language = "Python"

# Defining a floating-point variable 'version' with the value 3.6.
version = 3.6

# Converting the 'version' variable to a string using the "%" string formatting operator.
str_version = "%s" % version

# Printing a concatenated string with information about the programming language and its version.
print('Language: ' + language + ',' + ' Version: ' + str_version)

Sample Input:
language = "Python"
version = 3.6

Sample Output:

Language: Python, Version: 3.6

Explanation:

Here is a breakdown of the above Python code:

  • Variable definition:
    • language = "Python": Assigns the string value "Python" to the variable 'language'.
  • Variable definition:
    • version = 3.6: Assigns the floating-point value 3.6 to the variable 'version'.
  • String formatting:
    • str_version = "%s" % version: Converts the floating-point 'version' variable to a string using the "%" string formatting operator.
  • Print Statement:
    • print('Language: ' + language + ',' + ' Version: ' + str_version): Prints a concatenated string containing information about the programming language and its version. The 'str_version' variable is used for the version part.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to print Emojis using unicode characters or CLDR (Common Locale Data Repository ) short names.
Next: Write a Python program to find the largest and smallest digit of a given number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.