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

Yaz R
seal-mask
.a{fill-rule:evenodd;}techdegree
Yaz R
Python Web Development Techdegree Student 1,262 Points

After printing my string, how would i be able to add spaces without having to type the long string all over again?

This is my code:

learning="I love to learn many languages " learning.upper() 'I LOVE TO LEARN MANY LANGUAGES'

Now, what if I wanted to add spaces (escape sequence \n) anywhere in between the string, without having to type the string all over again?

blank_str = " " print("I love learning {} languages".format(blank_str*20))

1 Answer

Steven Parker
Steven Parker
222,348 Points

Strings are immutable, but you can replace it with a new string made using portions of the existing one:

learning = "I love to learn many languages"
learning = learning[:15] + "\n" + learning[16:]
print(learning)

I love to learn
many languages