w3resource

Python: Display formatted text as output

Python String: Exercise-26 with Solution

Write a Python program to display formatted text (width=50) as output.

Sample Solution:-

Python Code:

import textwrap
sample_text = '''
  Python is a widely used high-level, general-purpose, interpreted,
  dynamic programming language. Its design philosophy emphasizes
  code readability, and its syntax allows programmers to express
  concepts in fewer lines of code than possible in languages such
  as C++ or Java.
  '''
print()
print(textwrap.fill(sample_text, width=50))
print()

Sample Output:

 Python is a widely used high-level, general-                                                               
purpose, interpreted,   dynamic programming                                                                   
language. Its design philosophy emphasizes   code                                                             
readability, and its syntax allows programmers to                                                             
express   concepts in fewer lines of code than                                                                
possible in languages such   as C++ or Java.    

Flowchart:

Flowchart: Display formatted text as output

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to create a Caesar encryption.
Next: Write a Python program to remove existing indentation from all of the lines in a given text.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Getting rid of unwanted characters:

You can get rid of whitespaces or any specific character using strip methods in Python. You can use either plain strip for both sides, lstrip for the left side and rstrip for the right side only.

str="+++Welcome python+++"
str.strip()

print(str)

Output:

+++Welcome python+++