How to write paragraphs and assign it to a variable/name in Python?

kafleZ
3 min readNov 18, 2023

You may have encountered challenges when you want to assign a paragraph (which has line breaks, different words like for, in , and, etc., in multiple).

Example code of the problem:

with open('test1.txt', 'w') as f:
data = '

The Zen of Python, by Tim Peters


Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

'

f.write(data)
f.close()

Sample output of above code:

  File "<ipython-input-63-d6afd08e0997>", line 2
data = '
^
SyntaxError: unterminated string literal (detected at line 2)

There may be other different ways to deal with this kind of error. Here is my approach to deal with this problem — by using “‘ text body”’ instead of

‘ text body’ or “text body”. Remember docstring used in functions and or class?

Example:

def my_function():
' ' 'Here you will relevant details concisely to make code understandable and more readable' ' '

return None

print(my_function.__doc__)

Output:

Here you will relevant details concisely to make code understandable and more readable 

Using ‘“ text body ’” to solve our problem.

with open('test1.txt', 'w') as f:
data = '''

The Zen of Python, by Tim Peters


Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

'''

f.write(data)
f.close()

Lets see if it worked.

with open('test1.txt', 'r') as f:
data = f.read()
f.close()
print(data)

Output:



The Zen of Python, by Tim Peters


Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

It worked!

Have fun with your coding :)

--

--