Assemble the Words Into a Poem
00:00 You’ve made it all the way here. You got all the words that you need to generate an endless amount of poems. Not quite endless, but there’s a lot of combinations that these amount of words that you have in there could create.
I’ll assign it to a variable that I’ll call
poem and then start off with a string, right? So the first line here, I’m actually just going to copy that because in the way that I named my variables in here, I don’t actually need to change anything here.
This is just going to insert
article because f-strings work with these curly braces to interpolate values into it. So this is going to be replaced with
"An", depending on what we calculate it should be.
01:47 But I need multiple lines here. So I could just go with a multi-line string, which might be an easy way to do this. But I want to show you something. Actually, you can use multiple lines to write your strings if you wrap them inside of parentheses.
02:08 This is just going to get stuck together. But Python, basically it’s, it’s going to pretend that this is just one long string, which means that here I’m going to need to add some newline characters.
F5 save and run.
A Fragrant Badger. And then the poem starts. Cool. And something that’s interesting with this, using parentheses for strings like that, is you may have noticed that the syntax looks similar to creating a tuple, right?
02:55 And if you actually put a comma here, then you would create a tuple out of two strings instead of concatenating the string together. So this is why the commas are the important part about tuples and not actually the parentheses.
03:21 I’m going to put this in a new line because Python’s going to stick it together, like I mentioned, and then it’s going to stay easier to read. So this is still line two because I haven’t added a newline character at the end.
A glistening eagle hauls in the
serene elephant, whatever that means. But one thing that I’ve noticed here is already that, because Python just sticks these strings together, I also need to add a space here at the end for it to read correctly.
Become a Member to join the conversation.