Program to Trim Whitespace From a String

Python Program to Trim Whitespace From a String

Strings:

A string in Python is an ordered collection of characters that is used to represent and store text-based data. Strings are stored in a contiguous memory area as individual characters. It can be accessed in both forward and backward directions. Characters are merely symbols. Strings are immutable Data Types in Python, which means they can’t be modified once they’ve been generated.

Trim in Python:

What does it mean to trim a string, and how do you accomplish it in Python? Trimming a string refers to the process of removing whitespace from around text strings.

Given a string, the task is to trim the white spaces in the given string.

Examples:

Example1:

Input:

given_string = "    B     T  e  c  h   G    e       e      k   s       "

Output:

Before removing white spaces given string=   B Tec  h G e         e    k s    
after removing white spaces given string= BTechGeeks

Removing trialing and leading whitespaces

Example3:

Input:

given_string = "            croyez              "

Output:

Before removing white spaces given string=         croyez             
after removing white spaces given string= croyez

Example2:

Input:

given_string = "          BtechGeeks          "

Output:

Before removing white spaces given string=           BtechGeeks               
after removing white spaces given string= BtechGeeks

Program to Trim Whitespace From a String in Python

There are several ways to trim the whitespace from the given string some of them are:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Using replace() function

Replace() function:

The replace() method copies the string and replaces the previous substring with the new substring. The original string has not been altered.

If the old substring cannot be retrieved, the copy of the original string is returned.

We can use the replace() function in python to trim the white spaces in Python.

We need to replace the white space with empty string to achieve our task.

Below is the implementation:

# given string
given_string = "  B Tec  h G e        e    k s    "
# printing the original string before removing white spaces
print("Before removing white spaces given string=", given_string)
# removing white spaces from the given string
# using replace() function
# we replace white space with empty string

given_string = given_string.replace(" ", "")
# printing the original string before after white spaces
print("after removing white spaces given string=", given_string)

Output:

Before removing white spaces given string=   B Tec  h G e        e    k s    
after removing white spaces given string= BTechGeeks

Method #2:Using split() and join() functions

Below is the implementation:

# given string
given_string = "          B  t      echG   eeks               "
# printing the original string before removing white spaces
print("Before removing white spaces given string=", given_string)
# removing white spaces from the given string
given_string = "".join(given_string.split())
# printing the original string before after white spaces
print("after removing white spaces given string=", given_string)

Output:

Before removing white spaces given string=           B  t      echG   eeks               
after removing white spaces given string= BtechGeeks

Removing only leading and Trailing whitespaces:

Method #3:Using strip()

String in Python. The strip() function, as the name implies, eliminates all leading and following spaces from a string. As a result, we may use this method in Python to completely trim a string.

Syntax:

string.strip(character)

Character : It is a non-mandatory parameter. If the supplied character is supplied to the strip() function, it will be removed from both ends of the string.

Below is the implementation:

# given string
given_string = "          BtechGeeks               "
# printing the original string before removing white spaces
print("Before removing white spaces given string=", given_string)
# removing white spaces from the given string
# removing trailing and leading white spaces

given_string = given_string.strip()
# printing the original string before after white spaces
print("after removing white spaces given string=", given_string)

Output:

Before removing white spaces given string=           BtechGeeks               
after removing white spaces given string= BtechGeeks

strip() removes the leading and trailing characters from a string, as well as whitespaces.

However, if the string contains characters such as ‘\n’ and you wish to remove only the whitespaces, you must specifically mention it in the strip() method, as seen in the following code.

Explanation:

Here the regular Expression removes only the leading and trailing whitespaces from the given string

Method #4:Using Regex

We can remove leading and trailing whitespaces in regex as shown below:

Below is the implementation:

# importing regex
import re
# given string
given_string = "       BtechGeeks        "
# printing the original string before removing white spaces
print("Before removing white spaces given string=", given_string)
# using regex to remove trailing and leading white spaces from the given string
given_string = re.sub(r'^\s+|\s+$', '', given_string)
# printing the original string before after white spaces
print("after removing white spaces given string=", given_string)
Before removing white spaces given string=        BtechGeeks        
after removing white spaces given string= BtechGeeks

Explanation:

Here the regular Expression removes only the leading and trailing whitespaces from the given string
Related Programs: