This function makes you wonder why you need to change the output to make it look good, which is why you rarely see examples of using the textwrap (Python Document) function in a typical lecture or class.

textwarp

The reason I’m covering this library in a post is that I came across the official Python documentation and thought, “Oh, I’ll use this when I need it.”

Currently, I’m receiving reports via Gmail, Slack, and Telegram for personal use. With a few small modifications to the output, I’ve found that it becomes much more readable.

textwrap Code & Output

import textwrap

long_text = "In the journey of achieving your dreams, \
remember that success is not final, failure is not fatal: \
it is the courage to continue that counts. \
Every setback is a chance to learn and grow, \
and every victory is just a step towards your next challenge. \
Stay resilient and keep moving forward."

wrapped_text = textwrap.fill(long_text, width=60)
print(wrapped_text)

# output:
# In the journey of achieving your dreams, remember that
# success is not final, failure is not fatal: it is the
# courage to continue that counts. Every setback is a chance
# to learn and grow, and every victory is just a step towards
# your next challenge. Stay resilient and keep moving forward.

wrapped_list = textwrap.wrap(long_text, width=60)
for line in wrapped_list:
    print(line)

# ouput:
# In the journey of achieving your dreams, remember that
# success is not final, failure is not fatal: it is the
# courage to continue that counts. Every setback is a chance
# to learn and grow, and every victory is just a step towards
# your next challenge. Stay resilient and keep moving forward.

wrapped_text = textwrap.fill(long_text, width=60)

# Indentation
indented_text = textwrap.indent(wrapped_text, prefix='    ')
print(indented_text)

# output :
#     In the journey of achieving your dreams, remember that
#     success is not final, failure is not fatal: it is the
#     courage to continue that counts. Every setback is a chance
#     to learn and grow, and every victory is just a step towards
#     your next challenge. Stay resilient and keep moving forward.


# Indentation delete
dedented_text = textwrap.dedent(indented_text)
print(dedented_text)

# output :
# In the journey of achieving your dreams, remember that
# success is not final, failure is not fatal: it is the
# courage to continue that counts. Every setback is a chance
# to learn and grow, and every victory is just a step towards
# your next challenge. Stay resilient and keep moving forward.

# Reduce
shortened_text = textwrap.shorten(long_text, width=50, placeholder="...")
print(shortened_text)

# output :
# This is a very long piece of text that needs to...

# TextWrapper initial
wrapper = textwrap.TextWrapper(width=60, initial_indent='* ', subsequent_indent='  ')
wrapped_text = wrapper.fill(long_text)
print(wrapped_text)

# output:
# * In the journey of achieving your dreams, remember that
#   success is not final, failure is not fatal: it is the
#   courage to continue that counts. Every setback is a chance
#   to learn and grow, and every victory is just a step
#   towards your next challenge. Stay resilient and keep
#   moving forward.

In my professional life, I used to view readability as an unnecessary task.

It felt like it slowed things down, added extra work, and sometimes even distorted the message’s meaning.

However, as a service provider, I now see it as an effective way to enhance quality.

Currently, I receive reports after collecting news or information, summarizing it, and removing duplicates. While this readability function may seem unnecessary for reports viewed in Gmail, it makes a noticeable difference on mobile platforms like Slack or Telegram, where readability becomes crucial.

(That said, including hyperlinks can sometimes make the underline look awkward, but it’s a minor issue.)

This shift in perspective is something I’ve been reflecting on personally over the past year. I believe this is partly because my previous company was very formal and disciplined, whereas my current workplace is more relaxed and fast-paced.

This has made me think about the trade-off between “speed first” and “discipline and formality first.” It’s a fundamental choice for anyone shaping an organization’s culture.

Personally, I find myself most comfortable in a balance—where speed is prioritized but doesn’t completely overshadow a degree of formality and structure.

By Mark

-_-

Leave a Reply

Your email address will not be published. Required fields are marked *