Can I convert numbers to strings?

Yes, you can convert numbers to strings in Python using the str() function. Here’s how you can do it:

Example:

# Converting integer to string
age_int = 25
age_str = str(age_int)
print(f"Age: {age_str}")  # Outputs: Age: 25
print(type(age_str))      # Outputs: <class 'str'>

# Converting float to string
height_float = 1.75
height_str = str(height_float)
print(f"Height: {height_str} meters")  # Outputs: Height: 1.75 meters
print(type(height_str))                 # Outputs: <class 'str'>
  • The str() function takes a number (integer or float) and converts it to a string format. This is useful when you need to concatenate numbers with other strings or display them in a specific format.

0 Comments

no data
Be the first to share your comment!