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

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.

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

Could you mark this solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.