(Updated answer for fstrings) For big string like what you have do a few things.
First use """ """ instead of trying to build it the way you are.
You should then use preferably fstrings (Python 3.6+)
return f"""The GC content for non retained introns is {avg_gc(nr)}
The GC content for retained introns is {avg_gc(r)}
The average length of non retained introns is {intlength(nr)}
The average length of retained introns is {intlength(r)}
The percent of non retained introns divisible by 3 is {intdiv(nr)}
The percent of retained introns divisible by 3 is {intdiv(r)}.
"""
Older versions of Python can use:
return """The GC content for non retained introns is {nr_gc}
The GC content for retained introns is {r_gc}
The average length of non retained introns is {nr_avglen}
The average length of retained introns is {r_avglen}
The percent of non retained introns divisible by 3 is {nr_percdiv}
The percent of retained introns divisible by 3 is {r_percdiv}.
""".format(r_gc = avg_gc(r),
nr_gc = avg_gc(nr),
r_avglen = intlength(r),
nr_avglen = intlength(nr),
r_percdiv = intdiv(r)
nr_percdiv = intdiv(nr))
Answer from Michael Robellard on Stack OverflowVideos
Hello,
I have a simple question about returning a string from a python function.
Say we have this simple funciton.
def min_to_hour(x):
hours=x/60
return (x , 'minutes', 'is' , hours , 'hours')
print(min_to_hour(60))
The results I get is (60, 'minutes', 'is', 1.0, 'hours')
However, I need the program to output only: 60 minutes is 1 hour.
How can I do this?
You define a function with a parameter s. That function immediately throws that value away and asks for input. You are calling a function with the same name as that parameter, and sending it an argument of the function name. That doesn't make any sense.
def addExclamation(s):
new_string = s + "!"
return new_string
print(addExclamation('Hello'))
Or:
def addExclamation():
s = input("please enter a string")
new_string = s + "!"
return new_string
print(addExclamation())
You have mixed up the function and the argument here:
print(s(addExclamation))
And, you probably meant to read the input outside of a function and pass the string into:
def addExclamation(s):
new_string = s + "!"
return new_string
s = input("please enter a string")
print(addExclamation(s))
A few things:
- Python spacing matters, so don't forget to indent!
- Check your
printsyntax. Everything you're printing should be inside the parens
One form of correct syntax would be:
def fnc2(a, b, c):
total = (a + b) * c
print("The result is: " + str(total))
You have to properly indent the code to function, also the return statement ends the function.
def fnc2(a,b,c):
total=(a+b)*c
return str(total)
print("The result is: " + fnc2(10, 20, 30))
Also, if you want to convert the result of your computation to a string for printing only, you should not return a string from your function, but rather do the conversion when you want to print the result.
def fnc2(a,b,c):
total=(a+b)*c
return total
print("The result is: " + str(fnc2(10, 20, 30)))