How can I print a sentence with the calculation?

I tried to type this:

quilt_width = 8
quilt_length = 12
print("total squares needed:" + quilt_width * quilt_length )

To show the value in the same line as the text without using 2 prints.

And I got the following error:

Traceback (most recent call last):
  File "", line 3, in <module>
    print("total squares needed:" + quilt_width * quilt_length )
TypeError: Can't convert 'int' object to str implicitly

I’m not sure what I did wrong. What is the correct syntax to achieve what I wanted?

Am I getting ahead of myself and this is something I will learn down the line?

quilt_width * quilt_length gives an integer (96). Trying to concatenate str + integer gives errors, as you can see in the error:

TypeError: Can't convert 'int' object to str implicitly

Can you explain more please?

What’s the correct way to write it then?

Is using two prints the only way?

1 Like

You use + to attempt string concatenation, but you don’t have two strings. Thus you get an error.

So either you will need to convert the integer to a string, so you can stick with your current approach (using +).

Or the must be another way to solve this, but i don’t know what you have learned. Have you learned string Formatting Operator? Or the string .format() method? Or the use of a comma?


No, not yet.

I’m still at the very beginning of Python.

So far, I learner these things:

  • Comments
  • Print
  • Strings
  • Variables
  • Errors
  • Numbers
  • Calculations
  • Changing Numbers

I will google the error I got and try to understand what happened, you already gave me some hints.

I guess I still lack the basics, I will understand what I did wrong after few more lessons.

Thank you for your time.

1 Like

so you had string and numbers, that is good.

The problem is that using + to add number and string together, will result in a TypeError, you can add these two types together. these types don’t go together, just like certain things don’t go together.

you can use a comma (,) instead of a + to overcome your problem. I had no idea this exercises was so early in a course. Had i known, i would have simplified my answer. Sorry about that


This makes more sense to me now as a newbie hahaha

Thanks, I will try your solution and Google to learn more about it.

Thanks again.

6 days later, I understand the error I made.

I was trying to add a number to a string, like saying 8 + trees equals what? Which doesn’t make any sense logically.

I changed my syntax to since you mentioned a comma:

quilt_width = 8
quilt_length = 12
print("total squares needed:" , quilt_width * quilt_length )

And it worked just fine.


Found comma(,) function was useful. Thanks @stetim94

1 Like

If i want to print a str along with a calculation, then how do i do it?
i.e. like
quilt_length = 12
quilt_length = 8
squares = “no. of squares needed now”
print(squares + (quilt_length*quilt_width))

This shows a TypeError, how to rectify it?

you could use format:

or f-string would be even better, but that requires python 3.6+

1 Like

Use a comma(,) instead of a +
That works.

Thank you, bro

1 Like

I later also replied this:

which i can highly recommend

oh i dont no im sorey

What if you convert the evaluated integer to a string by wrapping the result in the str() function, and concatenated that? This would explicitly say that you want to convert the integer to a string so you can concatenate it with the sentence.

print("Total squares needed: " + str(quilt_width * quilt_length))

si quieres concatenar texto con numero,necesitas convertir el numero a texto, con la funcion str() ejemplo :
print("total de cuadrados necesarios: " + str(quilt_width * quilt_length))

1 Like

@saidbout Thank you for asking this question, was real cool reading all the responses printing strings and integers together ! Its awesome you were thinking of combining the two at the time :+1:
@stetim94 @jimzoo9423838668 @ethanrogers484583614 @object1407568875
A year later, I appreciate all your help :pray:

The TypeError: can’t convert ‘int’ object to str implicitly is generated because ‘+’ (addition) does not work between an integer and a string. When concatenating strings with integers - you can’t directly stick together a string and an integer. So, in order to resolve this problem, you have to explicitly parse the integer to a string by the str() built-in function string + str(number) or use formatting to format your output.

There are 3 ways to resolve this problem.

print (‘your string:’ + str(value))
print (‘your string:’, value)
print (‘your string: {}’.format(value))