(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.

The output of data[‘value’] is an integer type value. For concatenating two strings using ‘+’, the integer value obtained needs to be converted to a string data type. The str() function is used for this conversion.

Without string conversion, python3 will give an error - " TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’ "

‘+’ is used to join or add

The “+” operator in a string is generally used to concatenate one string to the other

To combine or merge two or many string we used ‘+’ , by using this + we can use to concat two or more strings

To combine or merge two or many string we used ‘+’ , by using this + we can use to concat two or more strings

Here + is used with str function to concatenate (simply to join or add two string values together)

str() is a function in python which is used to convert any value into string format.
here,we want to convert value into strings so we use str() and also there are two strings in here so we use string cancetanation(+str)

Here +str is used to concatenate two strings

Python str() Function

The str() function converts the specified value into a string.

Here str used concatenate two strings which bee used in the code

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.

Hi!
I think the answer to your question is

+sign in this code is used for concatenation, which means joining two or more strings together.

str() is used is to ensure that the values of data[‘value’] and response_text[‘message’] are converted to strings before they are concatenated with the other strings. If these values were not converted to strings first, a TypeError would occur because the + operator cannot be used to concatenate strings with non-string values.

@exilecristiano We can put string inside print and for concatenation purposes. We use ‘+str()’ after string to be printed for joining string function output along with string to be displayed.

str() is a function which is used to convert any value into string format.
We manually need to convert value into strings so that we use str() functioning. It is one of the main function in python.

The str() function converts values to a string form so they can be combined with other strings.

pi = 3.14
##text = 'The value of pi is ’ + pi ## NO, does not work
text = 'The value of pi is ’ + str(pi) ## yes