Python Tutorial 04: String concatenation and conversion in Python

From my previous post: Python Tutorial 03 : basic arithmetic operator and create the first Python Function, we have directly populated the result into the screen using the print function with multiple arguments. Should we concatenate the result into one single string before populate it to the screen?

Yes.

Let’s review the code we have used from our previous example.

My first Python function - area_triangle

Python use the “+” operator to concentrate strings. However, it our case the variable “area”, “Base” and “Height” are actually an integer. Therefore, we have to convert the integer to string before we can concentrate the string. To convert an integer to a string, we can use the “Str()” function. The Str function is a build-in function that convert an object into string

Let’s put the output string into a new variable called “result”. Following is the program:

Python String concatenation and conversion

Following is the result:

Python String concatenation and conversion - result

area_triangle(20,40)
The are of a triangle of base 20 and height 40 is 400.0

Leave a Comment