[Tutor] New Line Problem
Peter Otten
__peter__ at web.de
Fri Feb 2 04:17:51 EST 2018
Anna Lapre wrote:
> I am having a problem with creating a new line in a return statement for a
> function. It has a mix of a bunch of strings and variables. Everything
> that I want included will print, except for the new lines. I have tried
> "\n" but with no luck. Do you have any recommendations? Thanks.
>
> http://py3.codeskulptor.org/#user301_SIdBE94Q8yi3qtM.py
> (it's the second function)
"\n" is indeed what you need:
>>> equal = "================"
>>> index = 2
>>> line1 = "helllo"
>>> line2 = "heillo"
>>> print(line1 + "\n" + equal[:index] + "^\n" + line2)
helllo
==^
heillo
Note that you can repeat a string with
>>> "=" * 3
'==='
>>> "ab" * 2
'abab'
which combined with str.format allows the following alternative solutions:
>>> print("{}\n{}^\n{}".format(line1, "=" * index, line2))
helllo
==^
heillo
# requires Python 3.6
>>> print(f"{line1}\n{'='*index}^\n{line2}")
helllo
==^
heillo
More information about the Tutor
mailing list