0

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.

4
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by sneekula
Featured Replies
  • Dont use [ICODE];[/ICODE]in python. [CODE]output = "The number is: " num = 12 print '%s%d' % (output,num) ''' The number is: 12 ''' [/CODE] [CODE]output = "The number is: " num = 12 output+=num print output[/CODE] Error message is pretty clear. [ICODE]TypeError: cannot concatenate 'str' and 'int' objects[/ICODE] [CODE]>>> output … Read More

1

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 by snippsat: n/a

0

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 by vegaseat: n/a

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.