Python string strip() method

Python String strip() Method

Description

The strip() method returns a copy of a string with all characters removed from the beginning and end of the string (whitespace characters are the default).

Syntax

The following is the syntax of the strip() method −

var.strip([chars]);

Parameters

  • chars − The characters to be removed from the beginning or end of the string.

Return Value

This method returns a copy of the string with all characters removed from the beginning and end of the string.

Example

The following example demonstrates the use of the strip() method.

var = " Hello Python "
var1 = var.strip()
print("Original string:", var)
print("String after removing spaces:", var1)

var = "0001234000"
var2 = var.rstrip('0')
print("Original string:", var)
print("String after removing '0':", var2)

var = 'this is string example....wow!!!'
var3 = var.strip('!')
print("Original string:", var)
print("String after removing '!':", var3)

Running this program will produce the following output −

Original string: Hello Python
String after removing spaces: Hello Python
Original string: 0001234000
String after removing '0': 0001234
Original string: this is string example....wow!!!
String after removing '!': this is string example....wow

Leave a Reply

Your email address will not be published. Required fields are marked *