How to remove all leading whitespace in string in Python?

PythonServer Side ProgrammingProgramming

A string is a collection of characters that can represent a single word or a whole sentence. Strings are easy to use in Python since they don't need to be declared explicitly and may be defined with or without a specifier.

For manipulating and accessing strings, Python provides built in functions and methods under the class “String”. Using these methods you can perform various operations on strings.

In this article, we will be focusing on how to remove all leading whitespace from a string in python.

Using the lstrip() function

The basic approach is to use the lstrip() function from the inbuilt python string library. The function lstrip() removes any unnecessary spaces that are present on the left of the string.

We have similar functions rstrip() and strip().

  • The function rstrip() removes all the spaces towards the right of the string.

  • The strip() function removes all the spaces including left and right of the string.

Example 1

In the example given below, we performed the removal of trailing spaces using the lstrip() method.

str1 = "Hyderabad@1234" print("Removing the trailing spaces") print(str1.lstrip())

Output

The output of the above example is,

Removing the trailing spaces
Hyderabad@1234

Example 2

In the example given below, we performed the removal of leading spaces using the rstrip() method.

str1 = "Hyderabad@1234 " print("Removing the leading spaces") print(str1.rstrip())

Output

The output of the above given example is,

Removing the leading spaces
Hyderabad@1234

Example 3

In the example given below, we performed the removal of both trailing and leading spaces using the strip() method.

str1 = "Hyderabad@1234" print("Removing both trailing and leading spaces") print(str1.strip())

Output

The output of the above given program is,

Removing both trailing and leading spaces
Hyderabad@1234

Using the replace() method

We can also use the replace() method from the string library to remove the leading spaces. In this approach, we will replace all the whitespaces with null characters (‘’).

The main drawback of this function is that the spaces in between the string are also removed, so it is not used mostly.

Example

Following is an example for this −

str1 = " Welcome to Tutorialspoint" print("The given string is: ",str1) print("After removing the leading white spaces") print(str1.replace(" ",""))

Output

('The given string is: ', '         Welcome to Tutorialspoint')
After removing the leading white spaces
WelcometoTutorialspoint

Using the join() and split() methods

Another approach is by using the join() method combined with the split() method. We will map empty spaces using this method and then replace them with no space using the spilt() method. There are no drawbacks to this method.

Example

In the example given below, we performed the removal of both trailing and leading spaces combining the join() method and split() method.

str1 = " Hyderabad@1234 " print("Removing both trailing and leading spaces") print(" ".join(str1.split()))

Output

The output of the above given program is,

Removing both trailing and leading spaces
Hyderabad@1234
raja
Updated on 19-Oct-2022 08:10:17

Advertisements