3 Methods to Trim string in Python

In this post, we are going to learn 3 Methods to Trim string in Python with examples. Python has three inbuilt function strip(), lstrip(), rstrip() to remove white space from the string. Python strip() function is used to remove leading and trailing white space.

3 Methods to Trim string in Python

  • strip() : Python strip() function for removing the leading and trailing whitespace including tab(\t)tab and return a new string.
  • lstrip() : Python lstrip() function to remove leading space from string and return new string
  • rstrip() : Python lstrip() function to remove trailig space from string and return new string

1. Python strip() to trim string


Python strip() function removes the leading and trailing white spaces from a string that includes the characters tab and returns a new string after removing white space.

Syntax

string.strip(character)

The character parameter is optional if the character is passed it remove the passed character from the start and end of the string. if not passed then by default it removes white space.

Trim leading and trialing spaces from Python string

In this example, we are Trimming leading and trialing spaces from Python string.

original_str = '  devenum'

print('original string :',original_str)

resut_string = original_str.strip()

print('string after remove leading and trialing space :',resut_string)

Output

original string :   devenum
string after remove leading and trialing space : devenum

Trim Special character from Python string

In this example, we are Trimming leading and trialing special character from Python string

original_str = '$$$$$devenum$$$$'s

print('original string :',original_str)

resut_string = original_str.strip('$')

print('string after remove leading and trialing charcater:',resut_string)


Output

original string : $$$$$devenum$$$$
string after remove leading and trialing charcater: devenum

Trim mutiple Special character from start and end of Python string

In this example, we are Trimming leading and trialing multiple special character from Python string

original_str = '--+devenum--+'

print('original string :',original_str)

resut_string = original_str.strip('-+')

print('string after remove leading and trialing character :',resut_string)



Output

original string: --+devenum--+
string after remove leading and trialing character: devenum

2. Python lstrip() function to trim string


Python lstrip() function returns a new string after removing the white space from the left side of a given string.

Syntax

string.lstrip(charcater)

The character parameter is optional if the character is passed it removes the passed character from the start and end of the string. if not pass then by default it removes white space.

Trim space from beginning of string

In this example, we are Trimming leading space from Python string

original_str = '  devenumcom'

print('original string :',original_str)

resut_string = original_str.lstrip()

print('string after removing space left side of string :',resut_string)

Output

original string :   devenumcom
string after removing space left side of string : devenumcom

Trim a special character from beginning of string

In this example, we are Trimming the leading special characters from the beginning of the Python string.

original_str = '$$$$$$devenumcom'

print('original string :',original_str)

resut_string = original_str.lstrip('$')

print('string after removing space left side of string :',resut_string)

output

original string : $$$$$$devenumcom
string after removing space left side of string : devenumcom

3.Python rstrip() to trim string


Python rstrip() function returns a new string after removing the white space from the right side of the string.

Syntax

string.rstrip(character)

The character is an optional parameter of rstrip() function. If passed then remove the given character from the right of the given string, if not passed then it removes the white space.

Trim space from end of string

In this example, we are Trimming the trailing space of the Python string.

original_str = 'devenum      '

print('original string :',original_str)

resut_string = original_str.rstrip(' ')

print('string after triming  trialing space :',resut_string)

Output

original string : devenum      
string after triming  trialing space : devenum

Trim a special character from end of string

In this example, we are Trimming the trailing special characters from Python string.

original_str = 'devenum$$$$$$$$$$$$$$$$$$$$$$$$$'

print('original string :',original_str)

resut_string = original_str.rstrip('$')

print('string after triming  trialing space :',resut_string)

Output

original string : devenum$$$$$$$$$$$$$$$$$$$$$$$$$
string after triming  trialing space : devenum

Trim multiple special character end of python string

In this example, we are Trimming the trailing multiple special characters from Python string.

original_str = 'devenum++++++-----$$$$$$$$$$$'

print('original string :',original_str)

resut_string = original_str.rstrip('+$-')

print('string after triming trialing charcaters :',resut_string)

Output

original string : devenum++++++-----$$$$$$$$$$$
string after triming trialing charcaters : devenum