(str) in python

print ("Sensor value is: " + str(data[‘value’]))
print("Response received from Mailgun is: " + str(response_text[‘message’]))

Why is +str is used in the above code and what is the use of it?

Here str is used because we need to combine or add two strings here, as the data received is in the form of a string.
So to add or combine two strings, we use string concatenation.
To concatenate two strings we use +str.

Hey,@exilecristiano
str() is a function in python which is used to convert any value into string format.See here
(if you have any idea about OOPs like java) + symbol is just used for concatenation of strings.To add a space between two strings you can write write-

print ("Sensor value is:",str(data[‘value’]))
print("Response received from Mailgun is:",str(response_text[‘message’]))

also this is valid statement in python.
it will add a space between two strings.
The result will be like this:

Sensor value is: Your_Sensor_Value
Response received from Mailgun is: Your_Response

Your_Sensor_Value is the sensor value you obtained.
Your_Response is the Response you got.
so,there is a space between Sensor value is: and Your_Sensor_Value
same for response received.
Concatenation of strings can be defined as adding(Combining) two or more strings to form a new string.
Note:Adding means concatenating or combining not mathematical addition.String Concatenation
a string can be a sentence,word or a name etc.string is an array or combination of more characters.see here to know more about strings
For example: “Bolt” is a string.because it has 4 characters(Alphabets) B,o,l and t.
again “Iot” is a string.it has characters I,o and t.
if we combine or concat these two strings with a space it will form a final string that is “Bolt Iot”
code:

a="Bolt"
b="Iot"
print(a,b)

or

a="Bolt"
b="Iot"
print(a+" "+b)

Both Will give same results.

Hope This Helps!

1 Like

Python str () Function

  • Definition and Usage. The str () function converts the specified value into a string.
  • Syntax.
  • Parameter Values. The encoding of the objects.

Here str is used because we need to combine or add two strings here, as the data received is in the form of a string.
So to add or combine two strings, we use string concatenation.
To concatenate two strings we use +str.

here str command is using because some times python3 needs to be specified before doing any operation of same data type .I will avoid errors.