You could use textwrap module:
>>> import textwrap
>>> strs = "In my project, I have a bunch of strings that are read in from a file. Most of them, when printed in the command console, exceed 80 characters in length and wrap around, looking ugly."
>>> print(textwrap.fill(strs, 20))
In my project, I
have a bunch of
strings that are
read in from a file.
Most of them, when
printed in the
command console,
exceed 80 characters
in length and wrap
around, looking
ugly.
help on textwrap.fill:
>>> textwrap.fill?
Definition: textwrap.fill(text, width=70, **kwargs)
Docstring:
Fill a single paragraph of text, returning a new string.
Reformat the single paragraph in 'text' to fit in lines of no more
than 'width' columns, and return a new string containing the entire
wrapped paragraph. As with wrap(), tabs are expanded and other
whitespace characters converted to space. See TextWrapper class for
available keyword args to customize wrapping behaviour.
Use regex if you don't want to merge a line into another line:
import re
strs = """In my project, I have a bunch of strings that are.
Read in from a file.
Most of them, when printed in the command console, exceed 80.
Characters in length and wrap around, looking ugly."""
print('\n'.join(line.strip() for line in re.findall(r'.{1,40}(?:\s+|$)', strs)))
# Reading a single line at once:
for x in strs.splitlines():
print '\n'.join(line.strip() for line in re.findall(r'.{1,40}(?:\s+|$)', x))
output:
In my project, I have a bunch of strings
that are.
Read in from a file.
Most of them, when printed in the
command console, exceed 80.
Characters in length and wrap around,
looking ugly.
Answer from Ashwini Chaudhary on Stack OverflowMicrosoft Support
support.microsoft.com › en-us › office › send-code-snippets-in-a-message-in-microsoft-teams-5406fe2b-2771-4b9a-bd73-656914f6ab3c
Send code snippets in a message in Microsoft Teams - Microsoft Support
To send a code snippet in a chat or channel message, first click Format below the compose box, then select Code snippet in the format bar.
Videos
Textwrap Library with Two Examples | Python Standard Library
02:50
Wrapping text output in Python - YouTube
16:22
Python Textwrap Module Tutorial - Formatting Lines of Text - YouTube
00:41
How To Wrap Text In Python The Easy Way! - YouTube
01:23
Python HackerRank Solutions: Text Wrap - YouTube
02:32
Python-textWrap - YouTube
Python
docs.python.org › 3 › library › textwrap.html
textwrap — Text wrapping and filling
All wrapping options are taken from instance attributes of the TextWrapper instance. Returns a list of output lines, without final newlines. If the wrapped output has no content, the returned list is empty. ... Wraps the single paragraph in text, and returns a single string containing the wrapped paragraph. ... © Copyright 2001 Python Software Foundation.
Python
bugs.python.org › issue1859
Issue 1859: textwrap doesn't linebreak on "\n" - Python tracker
January 17, 2008 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/46167
W3Schools
w3schools.com › python › ref_module_textwrap.asp
Python textwrap Module
The textwrap module provides functions for wrapping and formatting plain text.
Martin Heinz
martinheinz.dev › blog › 108
Everything You Can Do with Python's textwrap Module | Martin Heinz | Personal Website & Blog
February 7, 2024 - # Ugly formatting: multiline_string = """ First line Second line Third line """ from textwrap import dedent multiline_string = """ First line Second line Third line """ print(dedent(multiline_string)) # First line # Second line # Third line # Notice the leading blank line... # You can use: multiline_string = """\ First line Second line Third line """ # or from inspect import cleandoc cleandoc(multiline_string) # 'First line\nSecond line\nThird line' By default, multiline strings in Python honor any indentation used in the string, therefore we need to use the ugly formatting shown in the first variable in the snippet above.
PyPI
pypi.org › project › textwrapper
textwrapper
JavaScript is disabled in your browser. Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
Top answer 1 of 9
128
You could use textwrap module:
>>> import textwrap
>>> strs = "In my project, I have a bunch of strings that are read in from a file. Most of them, when printed in the command console, exceed 80 characters in length and wrap around, looking ugly."
>>> print(textwrap.fill(strs, 20))
In my project, I
have a bunch of
strings that are
read in from a file.
Most of them, when
printed in the
command console,
exceed 80 characters
in length and wrap
around, looking
ugly.
help on textwrap.fill:
>>> textwrap.fill?
Definition: textwrap.fill(text, width=70, **kwargs)
Docstring:
Fill a single paragraph of text, returning a new string.
Reformat the single paragraph in 'text' to fit in lines of no more
than 'width' columns, and return a new string containing the entire
wrapped paragraph. As with wrap(), tabs are expanded and other
whitespace characters converted to space. See TextWrapper class for
available keyword args to customize wrapping behaviour.
Use regex if you don't want to merge a line into another line:
import re
strs = """In my project, I have a bunch of strings that are.
Read in from a file.
Most of them, when printed in the command console, exceed 80.
Characters in length and wrap around, looking ugly."""
print('\n'.join(line.strip() for line in re.findall(r'.{1,40}(?:\s+|$)', strs)))
# Reading a single line at once:
for x in strs.splitlines():
print '\n'.join(line.strip() for line in re.findall(r'.{1,40}(?:\s+|$)', x))
output:
In my project, I have a bunch of strings
that are.
Read in from a file.
Most of them, when printed in the
command console, exceed 80.
Characters in length and wrap around,
looking ugly.
2 of 9
16
This is what the textwrap module is for. Try textwrap.fill(some_string, width=75).
Qinqianshan
qinqianshan.com › python › py_module › textwrap-text-package-and-fill
【2】文件读写-8-2-文本包装和填充--textwrap - Sam' Note
January 27, 2016 - import textwrap dedented_text =textwrap.dedent(sample_text).strip() print textwrap.fill(dedented_text,initial_indent='',subsequent_indent=' ' * 4,width=50,) The textwrap module can beused to format text for output in situations wherepretty-printing is desired.
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › textwrap.TextWrapper.wrap.html
textwrap.TextWrapper.wrap — Python Standard Library
Reformat the single paragraph in ‘text’ so it fits in lines of no more than ‘self.width’ columns, and return a list of wrapped lines. Tabs in ‘text’ are expanded with string.expandtabs(), and all other whitespace characters (including newline) are converted to space
Text Editor
texteditor.co
Text Editor - Free App for Editing Text Files
Text Editor can open all text files types including TXT, CSV, HTML, XML, CSS and JSON as well as code files such as C++, Java, and Python.
PyPI
pypi.org › project › textwrap3
textwrap3 · PyPI
» pip install textwrap3