Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
"""