Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Types and Branching String Methods

Gurjeet Dhillon
Gurjeet Dhillon
458 Points

Unable to use "\n" for a new line within a string; I am pasting my code here, please let me know what's wrong about it.

"Dear {},\nHappy Birthday!!!\n\nThanks for being a part of MyTasker's family; You are one of our valueable members. Enjoy your day and keep rocking.\n\n\nBest,\n{}".format("Ada", "Gurjeet")

Here's the output: "Dear Ada,\nHappy Birthday!!!\n\nThanks for being a part of MyTasker's family; You are one of our valueable members. Enjoy your day and keep rocking.\n\n\nBest,\nGurjeet"

1 Answer

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

If you just show the string / variable storing the string, then yes, it shows the \n, but if you print() the string / variable storing the string then you will get the new lines

"Dear {},\nHappy Birthday!!!\n\nThanks for being a part of MyTasker's family; You are one of our valueable members. Enjoy your day and keep rocking.\n\n\nBest,\n{}".format("Ada", "Gurjeet")

# this will just show the string like this
"Dear Ada,\nHappy Birthday!!!\n\nThanks for being a part of MyTasker's family; You are one of our valueable members. Enjoy your day and keep rocking.\n\n\nBest,\Gurjeet"

# but if you print it
print("Dear {},\nHappy Birthday!!!\n\nThanks for being a part of MyTasker's family; You are one of our valueable members. Enjoy your day and keep rocking.\n\n\nBest,\n{}".format("Ada", "Gurjeet"))

# then you get new lines
"""
Dear Ada,
Happy Birthday!!!

Thanks for being a part of MyTasker's family; You are one of our valueable members. Enjoy your day and keep rocking.

Best,
Gurjeet
"""