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.
March 20th, 2009 at 7:01 pm
hey guys i need your help. i need a printed from a python for loop to display the character equivalent of values. for instance the value of 1 will give the character #, a value of 2 will give the character ##, a value of 3 will give the printed output ### and so on.
March 20th, 2009 at 8:42 pm
i = 3
k = ['#'] * i
print ”.join(k)
March 20th, 2009 at 9:07 pm
i don’t think you got my point. for eg.
for i in range(1,5,1):
print i
the result is
1
2
3
4
but i want to assign characters to these vales so i get
#
##
###
####
March 21st, 2009 at 7:24 am
As this “smell” like class assignment, the only thing I can advise you to do is to mix what you just wrote with what I put in my previous comment. Merge the two and you’ll find the answer.