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 Collections (Retired) Lists Redux Pop

Sahar Nasiri
Sahar Nasiri
7,454 Points

Breaking a string line

How does Kenneth break the string lines?

Ryan Ruscett
Ryan Ruscett
23,309 Points

I am not sure what you are asking? Are you asking how to break a string up by spaces, by character or some other separator?

Can you give an example of what you are looking for? Then we can definitely set you up with the "how".

Sahar Nasiri
Sahar Nasiri
7,454 Points

For example you write this line:

print("Imagine that the line is too long and you just wanna break this string like this" 
" but I want whole line stick together when I run this program")

Hi Sahar, I havn't heard it mentioned or taught, but it looks like he does this:

print("Hello", # it seems like putting a comma here allows it to work
            "world") # when I tried this "Hello world" prints out on one line

I'm glad you pointed that out cause I hate when my code runs off the edge of the editor. That will be handy to use.

7 Answers

Ryan Ruscett
Ryan Ruscett
23,309 Points

You have three options. 1 being what I think is most relevant on downward.

Option 1 Here I use print, and create two separate string literals. And add them with a plus. At the plus I do a return. So the second literal goes on another line. Breaking up the string in code, but yet printing it on one line.

>>> print ("this is a long string " +
... "way to long for a single line")
this is a long string way to long for a single line
>>>

Option 2

We can break a string up by creating variables. You can see how I broke the string down into variables, and was able to put it back together for the print. This also allows me flexibility of printing individual parts of the string at any given time. If that was a benefit.

>>> a = "This string is way to"
>>> b = "long and I wish it were a lot shorter"
>>> print(a + b)
This string is way tolong and I wish it were a lot shorter
>>>

Lastly, Option 3

You can break a string up in code using """ triple quotes. Except it also prints out in the same format as it was typed.

>>> print("""this is a line
... that I want to write on
... different lines""")
this is a line
that I want to write on
different lines
>>>

Are either of these three options what you are looking for? Let me know!

Thanks!

Thanks Ryan, that was some good information.

Ryan Ruscett
Ryan Ruscett
23,309 Points

Are you referring to the fact that he put the string on two different lines like this:

print("hello",
         "this is a test")

Are you asking why he did it like this?

I personally am curious what made it work, is it the comma? (I don't really care why he did it)

Ryan Ruscett
Ryan Ruscett
23,309 Points

Sure, that makes sense. Here is the main reason why he did it.

  1. So that the string didn't get to long.

When you code, and you chain methods together like you do in .csharp or java. Sometimes the strings can get very very long. For example:

print("this is a string, that is really long and when someone is reading this they might have to scroll to the right")

That is a long line, and when chaining methods together, it can get very long and hard to read. A good rule of thumb is to break up the code onto multiple lines.

print("This is a really long string",
        ("but it's a lot better to read")

And yes, you might notice I split it on the comma, that is what I need to do in order to return a line. It's like progressive, it's just what you do lol.

Now, there is a second way. The string above, although written on two lines, prints onto one line. Which may not be what you actually want. You might actually want to the code on two separate lines. This is accomplished by using three single or double quotes.

print(""" this will make the string
           keep it's formate
          when it's printed to the screen""")

The reason I used triple quotes """ instead of triple single quotes like ''' is because my string contains a single quote in it's. If I didn't have that "it's" single quote. I could of used three triple quotes.

Here is the two methods in all their glory.

>>> print("""this code will not print on one line.
... it will actually keep it's formating,
... which is pretty cool""")
this code will not print on one line.
it will actually keep it's formating,
which is pretty cool
>>> print("this code on the other hand",
... " is printed on a single line")
('this code on the other hand', ' is printed on a single line')
>>>

See how one gets printed on the same line despite being split up when done as two separate strings separated by a comma unlike the first one which prints it out keeping the line separations.

I should say there is also more than just these two ways to do this. You could use line breaks too but let's not beat a dead horse.

Does that makes sense?

Sahar Nasiri
Sahar Nasiri
7,454 Points

Ryan Ruscett, thanks for your respond.

See, in the 4:55 minutes of the video, Kenneth break the string lines with none of the ways you mentioned. I do not understand what just happened?

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey Sahar,

In programming, there are often several ways to accomplish the exact same task. Programming is an evolution. Think of it like this. Before big data and data science. Where I have to crunch millions and millions of abstract pieces of data to come up with a correlation. I could probably get away with basic math in most computer programs. When those same methods used to compute basic math, are used to compute basic math a million times as fast as possible. There is often need for a new way in which to do the same thing to enhance performance. This trend continues on and on. In the end, there ends up being multiple ways to do the same thing. Depending on what exactly you are doing, there may be a better way than it was done before. Some methods might have security implications. Methods such as eval() vs exec() can accomplish the same thing, but one is more secure than the other. When eval() was used, security wasn't on everyones mind. Although, one may write code using both of these methods in situations where security isn't an issue. Kenneth can't teach us all the ways to accomplish a single task, for we typically 90 percent of the time only need 1.

Does that help explain it? Just use whatever sticks with you or you find the easiest

Sahar Nasiri
Sahar Nasiri
7,454 Points

Hi :)

Thanks for your explanation. I knew other ways to break a line of strings, but I am really curious about how Kenneth is doing it. The way he did it is not the ways that I know. Do you know how he did it?

Sahar Nasiri
Sahar Nasiri
7,454 Points

John Larson, hi,

No, he did not use any comma, you can check it on the video. It makes me crazy to know it :(.

Hi Sahar, I tried both of these and they both work. And yes you are correct, Kenneth does not use a comma. But a comma seems to eliminate the need for the added space in "Hello "

print("Hello "
"world")

print("Hello", 
"world")

I thought Python was pickier about space, but this seems to work fine. They both print out on one line.

Sahar Nasiri
Sahar Nasiri
7,454 Points

John Larson Thank you so much.

I checked, and you're right, it is working. But I remember that I tried this before and it did not work out :)). Clearly, I am wrong :)).