How do you trim text in Python?

Today, We want to share with you trim in python.In this post we will show you trim in python, hear for 3 ways to trim a String in Python we will give you demo and example for implement.In this post, we will learn about Remove Last Character from String jQuery with an example.

Python Trim String – rstrip(), lstrip(), strip()

Example 1:

inp_str = "     [email protected]"
print("Users String:")
print(inp_str)
res = inp_str.strip()
print("\nString after trimming extra leading and trailing spaces:")
print(res)

Results

Users String:
     [email protected]
 
String after trimming extra leading and trailing spaces:
[email protected]

Example 2:

inp_str = "@@Laravl [email protected]@@@"
print("Users String:")
print(inp_str)
res = inp_str.strip('@')
print("\nString after trimming extra leading and trailing spaces:")
print(res)

Users String:
@@Laravl [email protected]@@@
 
String after trimming extra leading and trailing spaces:
Laravl PakainfoJD

NumPy strip() method

Example 3:

import numpy
 
arr = numpy.array([' PakainfoJD', 'Laravl  '])
print("Users Array:")
print(arr)
res = numpy.char.strip(arr)
print("Array after performing strip():")
print(res)

Users Array:
[' PakainfoJD' 'Laravl  ']
Array after performing strip():
['PakainfoJD' 'Laravl']

Example 4:

import numpy
 
arr = numpy.array([' PakainfoJD', 'Laravl  '])
print("Users Array:")
print(arr)
res = numpy.char.strip(arr, chars='Laravl')
print("Array after performing strip():")
print(res)

Users Array:
[' PakainfoJD' 'Laravl  ']
Array after performing strip():
[' PakainfoJD' '  ']

NumPy lstrip() method

Example

import numpy
 
arr = numpy.array(['@@!PakainfoJD', '@%*Laravl  '])
print("Users Array:")
print(arr)
res = numpy.char.lstrip(arr, chars="!%@*")
print("Array after performing lstrip():")
print(res)

Results
Users Array:
['@@!PakainfoJD' '@%*Laravl  ']
Array after performing lstrip():
['PakainfoJD' 'Laravl  ']

Example

import numpy
 
arr = numpy.array(['  PakainfoJD', ' Laravl'])
print("Users Array:")
print(arr)
res = numpy.char.lstrip(arr)
print("Array after performing lstrip():")
print(res)

Results
Users Array:
['  PakainfoJD' ' Laravl']
Array after performing lstrip():
['PakainfoJD' 'Laravl']

Python rstrip()

Example

inp_str = "[email protected]   "
print("Users String:")
print(inp_str)
print("Length of Users String:")
print(len(inp_str))
res = inp_str.rstrip()
print("\nString after trimming Extra trailing spaces:")
print(res)
print("Length of Users String after removing extra trailing spaces:")
print(len(res))

Results
Users String:
[email protected]   
Length of Users String:
20
 
String after trimming Extra trailing spaces:
[email protected]
Length of Users String after removing extra trailing spaces:
17

Example

inp_str = "[email protected]****"
print("Users String:")
print(inp_str)
print("Length of Users String:")
print(len(inp_str))
res = inp_str.rstrip("*")
print("\nString after trimming Extra trailing characters:")
print(res)
print("Length of Users String after removing extra trailing spaces:")
print(len(res))

Results
Users String:
[email protected]****
Length of Users String:
21
 
String after trimming Extra trailing characters:
[email protected]
Length of Users String after removing extra trailing spaces:
17

NumPy rstrip() method

Example

import numpy
 
arr = numpy.array(['  PakainfoJD  ', ' Laravl    '])
print("Users Array:")
print(arr)
res = numpy.char.rstrip(arr)
print("Array after performing rstrip():")
print(res)

Results
Users Array:
['  PakainfoJD  ' ' Laravl    ']
Array after performing rstrip():
['  PakainfoJD' ' Laravl']

Example

import numpy
 
arr = numpy.array(['  PakainfoJD****', ' Laravl!!'])
print("Users Array:")
print(arr)
res = numpy.char.rstrip(arr, chars="*!")
print("Array after performing rstrip():")
print(res)

Results
Users Array:
['  PakainfoJD****' ' Laravl!!']
Array after performing rstrip():
['  PakainfoJD' ' Laravl']

I hope you get an idea about python strip string after character.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.