Unable to concatenate integer in my "print" statement
Question:
I am unable to place an integer inside my print statement alongside a string, and concatenate them together.
pounds = input("Please type in your weight in pounds: ")
weight = int(pounds) * 0.45
print("You " + weight)
I thought that I would be able to put these together, why am I unable to?
Answers:
print("You %s" % weight)
or print("You " + str(weight))
Since you are trying to concat a string with an integer it’s going to throw an error. You need to either cast the integer back into a string, or print it without concatenating the string
You can either
a) use commas in the print function instead of string concat
print("You",weight)
b) recast into string
print("You "+str(weight))
Edit:
Like some of the other answers pointed out, you can also
c) format it into the string.
print("You {}".format(weight))
Hope this helps! =)
Another way is to use format strings like print(f"You {weight}")
Python is dynamically typed but it is also strongly typed. This means you can concatenate two str
s with the +
or you can add two numeric values, but you cannot add a str
and an int
.
Try this if you’d like to print both values:
print("You", weight)
Rather than concatenating the two variables into a single string, it passes them to the print
function as separate parameters.
Python doesn’t let you concatenate a string with a float. You can solve this using various methods:
Cast the float to string first:
print("You " + str(weight))
Passing weight
as a parameter to the print function (Python 3):
print("You", weight)
Using various Python formatting methods:
# Option 1
print("You %s" % weight)
# Option 2 (newer)
print("You {0}".format(weight))
# Option 3, format strings (newest, Python 3.6)
print(f"You {weight}")
I am unable to place an integer inside my print statement alongside a string, and concatenate them together.
pounds = input("Please type in your weight in pounds: ")
weight = int(pounds) * 0.45
print("You " + weight)
I thought that I would be able to put these together, why am I unable to?
print("You %s" % weight)
or print("You " + str(weight))
Since you are trying to concat a string with an integer it’s going to throw an error. You need to either cast the integer back into a string, or print it without concatenating the string
You can either
a) use commas in the print function instead of string concat
print("You",weight)
b) recast into string
print("You "+str(weight))
Edit:
Like some of the other answers pointed out, you can also
c) format it into the string.
print("You {}".format(weight))
Hope this helps! =)
Another way is to use format strings like print(f"You {weight}")
Python is dynamically typed but it is also strongly typed. This means you can concatenate two str
s with the +
or you can add two numeric values, but you cannot add a str
and an int
.
Try this if you’d like to print both values:
print("You", weight)
Rather than concatenating the two variables into a single string, it passes them to the print
function as separate parameters.
Python doesn’t let you concatenate a string with a float. You can solve this using various methods:
Cast the float to string first:
print("You " + str(weight))
Passing weight
as a parameter to the print function (Python 3):
print("You", weight)
Using various Python formatting methods:
# Option 1
print("You %s" % weight)
# Option 2 (newer)
print("You {0}".format(weight))
# Option 3, format strings (newest, Python 3.6)
print(f"You {weight}")