w3resource

Python: Remove existing indentation from all of the lines in a given text


Remove indentation from text.

Write a Python program to remove existing indentation from all of the lines in a given text.

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.
    '''
text_without_Indentation = textwrap.dedent(sample_text)
print()
print(text_without_Indentation )
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: Remove existing indentation from all of the lines in a given text

For more Practice: Solve these Related Problems:

  • Write a Python program to remove all leading whitespace from each line in a multi-line string.
  • Write a Python program to use textwrap.dedent() to eliminate common indentation from a block of text.
  • Write a Python program to iterate over the lines of a text and strip indentation using str.lstrip().
  • Write a Python program to normalize indentation by removing extra spaces at the beginning of each line in a given text.

Go to:


Previous: Write a Python program to display formatted text (width=50) as output.
Next: Write a Python program to add a prefix text to all of the lines in a string.

Python Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.