python to string method code example

Example 1: how to convert to string in python

str(integer_value)

Example 2: to string python

x = 2.718
strX = str(x)

Example 3: string representation of object python

#!/bin/python3

import math
import os
import random
import re
import sys

class Car:
    def _init_(self,speed,unit):
        self.speed=speed
        self.unit=unit
    def __str__(self):
        return "Car with the maximum speed of {} {}".format(self.speed,self.unit)  

class Boat:
    def _init_(self,speed):
        self.speed=speed
    def __str__(self):
        return "Boat with the maximum speed of {} knots".format(self.speed)