Python Practice › String Programs › Program 62

Strip spaces

EasyString Programs · 62 of 106

Write a Python program to remove leading and trailing spaces.

Advertisement
Input
"    sqaltoolspythonfun     "
Output
'sqaltoolspythonfun' 'sqaltoolspythonfun     ' '    sqaltoolspythonfun'

Approach

  1. strip(); lstrip() / rstrip() for one side.

Solution

Python
text = "    sqaltoolspythonfun     "print(repr(text.strip()), repr(text.lstrip()), repr(text.rstrip()))
Output
'sqaltoolspythonfun' 'sqaltoolspythonfun     ' '    sqaltoolspythonfun'

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Leave a Comment