Hello guys. I just want to ask how to convert this code to python. I am just a beginner in this code.

String output = "The number is: ";
int num= 12;

output+=num;

System.out.println(output);

in my python code, i try to do this:

output = "The number is: ";
num = 12;

output+=num;
print output;

but i get an error. :( . I also tried this:

output = "The number is: ";
num = 12;

output= output, num;
print output;

but i also get an error.

Dont use ; in python.

output = "The number is: "
num = 12

print '%s%d' % (output,num)
'''
The number is: 12
'''
output = "The number is: "
num = 12

output+=num
print output

Error message is pretty clear. TypeError: cannot concatenate 'str' and 'int' objects

>>> output = "The number is: "
>>> type(output)
<type 'str'>
>>> num = 12
>>> type(12)
<type 'int'>
>>> #As you see we have a string and integer
>>> output + num

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    output + num
TypeError: cannot concatenate 'str' and 'int' objects

>>> str(num)  #convert to string
'12'
>>> output + str(num) 
'The number is: 12'
>>>

Edited 6 Years Ago by snippsat: n/a

You could get away with this ...

output = "The number is: ";
num = 12;

print output, num;

Note that the end of statement-line marker ';' is optional in Python, and can be used to put two statement on one line.

Edited 6 Years Ago by vegaseat: n/a

@snippat : This is what I am looking for. Thanks for the help.
@vegaseat : Thank you also for the help.

This article has been dead for over six months. Start a new discussion instead.