Python Pandas: Input numeric value on string message

Question:

Hi I want to input the computed numeric value on my MESSAGE variable. So basically I computed first my numeric value and then put it on a message string. Below is my code.

#Compute numeric value
LOCATIONS_COUNT = len(loc_df)
#Add the computed numeric value to the message
MESSAGE = "The total locations is LOCATION_COUNT"
print(MESSAGE)
#This was the result
The total locations is LOCATION_COUNT

Basically I need the result to output the numeric value for LOCATION_COUNT. For example the value is 10. Then the MESSAGE should be like this.

The total locations is 10
Asked By: Bustergun

||

Answers:

There are multiple ways to accomplish this. You could cast your int to string:

#Add the computed numeric value to the message
MESSAGE = "The total locations is "+ str(LOCATION_COUNT)
print(MESSAGE)

Or use some sort of string formatting, like this:

#Compute numeric value
LOCATION_COUNT = len(loc_df)
#Add the computed numeric value to the message
MESSAGE = f'The total locations is {LOCATION_COUNT}'
print(MESSAGE)

Or this:

#Compute numeric value
LOCATION_COUNT = len(loc_df)
#Add the computed numeric value to the message
MESSAGE = "The total locations is "
print("{}{}".format(MESSAGE, LOCATION_COUNT))
Answered By: Sabsa

Use Format :

LOCATIONS_COUNT = len(loc_df)
MESSAGE = "The total locations is {}".format(LOCATIONS_COUNT)
print(MESSAGE)

or

MESSAGE = "The total locations is {}".format(len(loc_df))
print(MESSAGE)
Answered By: Mokey D Luffy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.