Hi,
I have just started to learn python. I have installed and set the environment variables as per documentation. And from command prompt, I execute some python script,too. But I can't execute multiple line script, if the script contains any function. For example, see the following code -

>>>a,b=0,1
>>>print a,b

It successfully prints the value of a and b.

But if I want to execute following types of script -

str1 = "Hello Monty Python!"
str2 = str1.replace('M', 'Spam')
str3 = str2.replace('P', 'Spam')
print( str1 )
print( str2 )
print( str3 )

I am not able to write this type of script in command line.
When I write the first Print statement i.e. print(str1) and press enter,
it prints the value of the str1.
Please suggest how to write this type of code in command line.

Thanks and regards,

Thanks Tony, I will try this one.
But it is not possible having three different Print statement as given in my code snippet?

print('\n'.join((str1, str2, str3)))

maybe?

Edited 6 Years Ago by java_programmer: n/a

Maybe like this.

str1 = "Hello Monty Python!"
str2 = str1.replace('M', 'Spam')
str3 = str2.replace('P', 'Spam')
def print_str(str1, str2, str3):
    print(str1)
    print(str2)
    print(str3)
print_str()

or

str1 = "Hello Monty Python!"
str2 = str1.replace('M', 'Spam')
str3 = str2.replace('P', 'Spam')
print('%s\n%s\n%s' % (str1, str2, str3))

Edited 6 Years Ago by Beat_Slayer: n/a

>>> from __future__ import print_function
>>> str1 = "Hello Monty Python!"
>>> str2 = str1.replace('M', 'Spam')
>>> str3 = str2.replace('P', 'Spam')
>>> print( str1 ); print( str2 ) ; print( str3 )
Hello Monty Python!
Hello Spamonty Python!
Hello Spamonty Spamython!
This article has been dead for over six months. Start a new discussion instead.