Home > other >  Python Pandas: Input numeric value on string message
Python Pandas: Input numeric value on string message

Time:07-29

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

CodePudding user response:

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))

CodePudding user response:

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)
  • Related