pythonendl

printing results in multiple lines in python facing an error


In C++, we simply use std::endl in order to print a result in multiple lines. How can I do the same in Python? I use '\n' and I face a problem.

If I wanted to write the code in C++, here's how I'd write it:

#include <iostream>
#include <math.h>

int main()
{
   int a,b;
   cin>>a>>b;
   cout<<a+b<<endl<<a*b<<endl<<pow(a,b)<<endl;
}

Solution

  • Use the sep keyword:

    num1 = int(input())
    num2 = int(input())
    
    print(num1+num2,num1 * num2,num1**num2, sep='\n')
    

    or simply comma seperate them:

    print(num1+num2, '\n', num1 * num2, '\n', num1**num2)
    

    In python + between str and int are not allowed, you could use f-strings if you are running python 3.6+:

    print(f"{num1 + num2}\n{num1 * num2}\n{num1**num2}")