1

I'm trying to print a new line while using % formatting. This is the code I have:

return '%s\n%s\n%s\nScore: %d' % (seqA, matches, seqB, score)

But that prints:

'ATTCGT\n||   |\nATCTAT\nScore: 2'

Is there a way to print new lines using this method?

4
  • You're confusing the diagnostics the Python shell prints (a representation, using the repr function, of the result of the previous line evaluated) with actual output of your script. Your code doesn't actually output anything. Commented May 10, 2013 at 2:34
  • 1
    I just tried it outside the Python IDLE program, and used 'print' and it worked. Sorry. Commented May 10, 2013 at 2:35
  • Don't be sorry, it's not an uncommon mistake. Commented May 10, 2013 at 2:37
  • The question is part of a pyschools challenge. 'Pairwise comparision of DNA sequences is a popular technique used in Bioinformatics. It usually involves some scoring scheme to express the degree of similarity. Write a function that compares two DNA sequences based on the following scoring scheme: +1 for a match, +3 for each consecutive match and -1 for each mismatch.' That's why I was using 'return'. I was struggling to get it right in their code editor so I tried IDLE. That was giving me the output in the OP, so I thought I was missing something. Commented May 10, 2013 at 2:43

2 Answers 2

1

Use print instead of return. print evaluates each expression and writes the result to standard output, whereas return merely echoes the returned object (in IDLE):

>>> seqA = 'ATTCGT'
>>> matches = '||   |'
>>> seqB = 'ATCTAT'
>>> score = 2
>>> print '%s\n%s\n%s\nScore: %d' % (seqA, matches, seqB, score)
ATTCGT
||   |
ATCTAT
Score: 2
Sign up to request clarification or add additional context in comments.

2 Comments

I feel like this answer is just inaccurate enough to be unhelpful, though it technically probably will solve his problem.
I've adjusted the answer to reflect more appropriate terminology. Thanks for the prompt.
0

You get the value correctly. But you need the print it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.