Zientzilaria

Covering the bioinformatics niche and much more

Python Printing Statement

| Comments

Just 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:

1
2
print "This is a"
print "test"

will print

This is a test

while

1
2
print "This is a",
print "test"

will print

This is a test

Of course Python’s print statement allows any programming escape character, 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

1
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

1
myDNA3 = myDNA + myDNA2

but instead we would use the print command as

1
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

1
print myDNA3 + " " + myDNA