Python printing statement
Section 1 January 15th, 2007Just an apart from the bioinformatics aspect of programming: Python’s print statement.
As in most computer languages Python allows an easy way to write to the standard output. Python’s print only accepts output of strings, and if the variable sent to it is not a string it is first converted and then output.
The print always put a linebreak ('\n' or "\n") at the end of the expression to be output, except when the print statement ends with a comma. For example:
print "This is a" print "test"
will print
This is a
test
while
print "This is a", print "test",
will print
This is a test
Of course Python’s print statement allows any programming escape characte, such as '\n' and '\t'.
Concatenating strings on output
To concatenate two strings on output there are two possible ways in Python. You can either separate the strings with a comma, like we did here
print myDNA, myDNA2
or you can use the “+” sign in roder to obtain almost the same result. This is similar to what was used here
myDNA3 = myDNA + myDNA2
but instead we would use the print command as
print myDNA3 + myDNA
In the latter case, both strings will not be separated by a space and will be merged. To get the same result you would have to concatenate an extra space between the strings like
print myDNA3 + " " + myDNA
February 16th, 2008 at 8:32 pm
Just to tell that you could use () for this examples, like:
print (myDNA3 + ” ” + myDNA)
It is not needed by now, but in Python 3.0 they will be mandatory.
February 16th, 2008 at 9:29 pm
Hi Sebastian
Thanks. I haven’t seen the requirements of Python 3 yet. Maybe it is a good time to check.