w3resource

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

Python String: Exercise-27 with Solution

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

Python Code Editor:

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

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.

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.

Python: Tips of the Day

Sorting a list of lists:

Consider we have a list of lists:

lst = [[3, 5], [6, 8], [4, 6], [5, 8], [6, 7], [5, 8]]

We can sort the list based on the first or second items of the inner lists by using the sort function with a lambda function.

lst.sort(key = lambda inner:inner[1])
print(lst)

Output:

[[3, 5], [4, 6], [6, 7], [6, 8], [5, 8], [5, 8]]

The list is sorted based on the second items. We can do the same with the first items just by changing the 1 to 0.

lst.sort(key = lambda inner:inner[0])
print(lst)

Output:

[[3, 5], [4, 6], [5, 8], [5, 8], [6, 8], [6, 7]]