What if i want output with string + integer? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What if i want output with string + integer?

Example a = 2*4 b = 'this is result:' i want output as this is result:8

17th Jun 2019, 5:51 PM
aung
7 Answers
+ 5
If you are using python, you can't concatinate strings and integers. To turn an integer into a string, use the str() function. print(b + str(a))
17th Jun 2019, 5:57 PM
Airree
Airree - avatar
+ 6
This depends on your language, take python for example, you would need to convert the integer to a string type using the str() function. Your code would look something like this: a = 2*4 b = 'this is result" print (b +" " + str(a)) just check for the conversion type for the language
17th Jun 2019, 5:58 PM
Anon
Anon - avatar
+ 5
You could do print(b, a) as well. Or one of these: print('%s %d' % (b, a)) print('{} {}'.format(b, a)) print('{0} {1}'.format(b, a)) print('{1} {0}'.format(a, b)) print(f'{b} {a}') print(__import__('string').Template('$b $a').safe_substitute({x: eval(x) for x in ('a', 'b')})) # <= probably shouldn't use this one
17th Jun 2019, 6:56 PM
Anna
Anna - avatar
+ 4
What language?
17th Jun 2019, 5:57 PM
Jackson O’Donnell
+ 2
Thanks All. String function helps a = 2*4 b = 'this is result:' c = print(b + str(a))
17th Jun 2019, 6:07 PM
aung
+ 1
I didn't understand
17th Jun 2019, 7:13 PM
Robel W
Robel W - avatar
0
As it is we can't able to add the string and integer, we need to mention the function str(b) to make the compiler to understand that it is a string not an integer,we do this to avoid run time compilation error. So hence,the correct notation for this output to occur is:. Print(b+str(a))
7th Aug 2019, 2:28 PM
Nithish Nithish
Nithish Nithish - avatar