New line character does not work in a return string in Python
Question:
Here in the below code, I try to do a calculation in a function and then I return all the values in a string to the main program body in Python. The issue here is the new line character n does not work with my code in the return statement.
What is the reason for that?
#create function
def program(value):
#creat variables
tip_percentage=10
tip_amount=0
final_bill=0
#calculations
tip_amount=value*tip_percentage/100
final_bill=value+tip_amount
#print output
x=("value -> n",value," Tip -> n",tip_amount,"Total value -> n",final_bill)
print(x)
return x
#Main body
value=int(input("Enter value : "))
value=program(value)
print(value)
Tried returning a value from a function. Then in the return statement the n which is the new line character does not work.
Answers:
It’s because you’re returning a tuple of values, instead of a string.
Try string concatenation:
x = "value -> n" + str(value) + " Tip -> n" + str(tip_amount) + " Total value -> n" + str(final_bill)
or try formatting like this:
x = "value -> n{} Tip -> n{} Total value -> n{}".format(value, tip_amount, final_bill)
The Tuple object you return x=("value -> n",value," Tip -> n",tip_amount,"Total value -> n",final_bill)
doesn’t not interpret escape sequences like n
it deal with it as normal string.
You can return a string instead.
x= "value -> n{}nTip -> n{}nTotal value -> n{}".format(value, tip_amount, final_bill)
x=("value -> n",value," Tip -> n",tip_amount,"Total value -> n",final_bill)
In the above line, you are writing your string as a tuple. You have to convert it to a string.
Here in the below code, I try to do a calculation in a function and then I return all the values in a string to the main program body in Python. The issue here is the new line character n does not work with my code in the return statement.
What is the reason for that?
#create function
def program(value):
#creat variables
tip_percentage=10
tip_amount=0
final_bill=0
#calculations
tip_amount=value*tip_percentage/100
final_bill=value+tip_amount
#print output
x=("value -> n",value," Tip -> n",tip_amount,"Total value -> n",final_bill)
print(x)
return x
#Main body
value=int(input("Enter value : "))
value=program(value)
print(value)
Tried returning a value from a function. Then in the return statement the n which is the new line character does not work.
It’s because you’re returning a tuple of values, instead of a string.
Try string concatenation:
x = "value -> n" + str(value) + " Tip -> n" + str(tip_amount) + " Total value -> n" + str(final_bill)
or try formatting like this:
x = "value -> n{} Tip -> n{} Total value -> n{}".format(value, tip_amount, final_bill)
The Tuple object you return x=("value -> n",value," Tip -> n",tip_amount,"Total value -> n",final_bill)
doesn’t not interpret escape sequences like n
it deal with it as normal string.
You can return a string instead.
x= "value -> n{}nTip -> n{}nTotal value -> n{}".format(value, tip_amount, final_bill)
x=("value -> n",value," Tip -> n",tip_amount,"Total value -> n",final_bill)
In the above line, you are writing your string as a tuple. You have to convert it to a string.