Skip to content

How to trim a string in Python (in 3 ways)

How to trim a string in Python

In this tutorial we will learn how to trim a string in python in various ways . Python string provides 3 built-in methods to trim a string . They are as follows –

  • Python string strip() method
  • Python string lstrip() method
  • Python string rstrip() method

Let us have a look about all this methods with examples –

Python String strip() method

It returns a new string after removing the leading and trailing characters from a given string .

Syntax of strip() method

string.strip([string])

Parameters of strip() method

string = Optional . It is a string specifying the set of characters which will be removed . Default value is 1 whitespace(" ") .

Return type of strip() method

It returns a new string .

Examples of Python String strip() method

Example 1: Remove all leading and trailing whitespaces from a string in Python

# declare a string
mystring = "      Coding Conception     "

# removing all whitespaces
mystring=mystring.strip()

# printing the string
print("-----",mystring,"-----")
----- Coding Conception -----
  • In the above code first we have declared a string named mystring as " Coding Conception "
  • Then we have applied strip() method on the string . As we did not give any argument so it has removed all leading and trailing whitespaces from the string .
  • From the output we have seen that all leading and trailing characters are removed from the string .

Example 2:

# declare a string
mystring = "aaaaCoding Conceptionaaa"

# removing all leading and trailing a
mystring=mystring.strip("a")

# printing the string
print("-----",mystring,"-----")
----- Coding Conception -----

In the above code we have used "a" as argument in the strip() method , so all the leading and trailing "a" is removed from the string .

Example 3:

# declare a string
mystring = "pythonpythonCoding Conceptionaaapythonpythonpython"

# removing all leading and trailing python
mystring=mystring.strip("python")

# printing the string
print("-----",mystring,"-----")
----- Coding Conceptionaaa -----

Python String lstrip() method

It returns a new string after removing all the leading characters from a given string .

Syntax of lstrip() method

string.lstrip([string])

Parameters of lstrip() method

string = Optional . It is a string specifying the set of characters which will be removed . Default value is 1 whitespace(" ") .

Return type of lstrip() method

It returns a new string .

Examples of Python String lstrip() method

Example 1:Remove all leading whitespaces from a string in Python

# declare a string
mystring = "      Coding Conception     "

# removing left whitespaces
mystring=mystring.lstrip()

# printing the string
print("-----",mystring,"-----")
----- Coding Conception      -----
  • In the above code first we have declared a string named mystring as " Coding Conception "
  • Then we have applied lstrip() method on the string . As we did not give any argument so it has removed all leading whitespaces from the string .
  • From the output we have seen that all leading characters are removed from the string .

Example 2:

# declare a string
mystring = "aaaaCoding Conceptionaaa"

# removing all a from left
mystring=mystring.lstrip("a")

# printing the string
print("-----",mystring,"-----")
----- Coding Conceptionaaa -----

In the above code we have used "a" as argument in the lstrip() method , so all the leading "a" is removed from the string .

Example 3:

# declare a string
mystring = "pythonpythonCoding Conceptionaaapythonpythonpython"

# removing all leading python
mystring=mystring.lstrip("python")

# printing the string
print("-----",mystring,"-----")
----- Coding Conceptionaaapythonpythonpython -----

Python String rstrip() method

It returns a new string after removing all the trailing characters from a given string .

Syntax of rstrip() method

string.rstrip([string])

Parameters of rstrip() method

string = Optional . It is a string specifying the set of characters which will be removed . Default value is 1 whitespace(" ") .

Return type of rstrip() method

It returns a new string .

Examples of Python String rstrip() method

Example 1: Remove all trailing whitespaces from a string in Python

# declare a string
mystring = "      Coding Conception     "

# removing right whitespaces
mystring=mystring.rstrip()

# printing the string
print("-----",mystring,"-----")
-----       Coding Conception -----
  • In the above code first we have declared a string named mystring as " Coding Conception "
  • Then we have applied rstrip() method on the string . As we did not give any argument so it has removed all trailing whitespaces from the string .
  • From the output we have seen that all trailing characters are removed from the string .

Example 2:

# declare a string
mystring = "aaaaCoding Conceptionaaa"

# removing all a from right
mystring=mystring.rstrip("a")

# printing the string
print("-----",mystring,"-----")
----- aaaaCoding Conception -----

In the above code we have used "a" as argument in the rstrip() method , so all the trailing "a" is removed from the string .

Example 3:

# declare a string
mystring = "pythonpythonCoding Conceptionaaapythonpythonpython"

# removing all trailing python
mystring=mystring.rstrip("python")

# printing the string
print("-----",mystring,"-----")
----- pythonpythonCoding Conceptionaaa -----

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply