Print function value in one line

Hi all! I don’t understand the output logic.

def miles_calculator(x):
print(x, str(“miles =”), end=" ")
return x * 1.609

a = miles_calculator(2)
print(a, “kilometers”)

RESULT
2 miles = 3.218 kilometers

Everything works I wanted, but I do not understand why it is displayed in one line print(x, str(“miles =”), end=" ") and return x * 1.609

Hello, @Boblik, and welcome to Python Software Foundation Discourse!

The official documentation may be helpful to you regarding the displayed output. Please see Built-in Functions: print(), and try to explain the output to us in terms of what you find there. We can then provide additional help, if necessary.

When posting code, please use backticks to format it for proper display. For example, your code can be formatted as follows:

```python
def miles_calculator(x):
     print(x, str("miles ="), end=" ")
     return x * 1.609

a = miles_calculator(2)
print(a, "kilometers")
```

With those lines of backticks in place, your code will appear in the following format, which displays indentation and other important details properly:

def miles_calculator(x):
    print(x, str("miles ="), end=" ")
    return x * 1.609

a = miles_calculator(2)
print(a, "kilometers")

py3 does not work to mark the language. You can use plain triple backticks for auto detection (which works sometimes) or use python with optional space:

``` python
...the code here...
```

Thanks for that information. I have now edited the post accordingly.

It is displayed on one line because you tell it to display on one line.

Your first print has end=" " so instead of printing a newline at the end, it prints a space.

If you remove the end=" " you will get the normal output

2 miles =
3.218 kilometers

but with it back in you get

2 miles = 3.218 kilometers

because the newline after the equals sign is replaced with a space.