What is txt strip () in python?

In this tutorial, we will learn about the Python String strip[] method with the help of examples.

The strip[] method returns a copy of the string by removing both the leading and the trailing characters [based on the string argument passed].

Example

message = '     Learn Python  '

# remove leading and trailing whitespaces print['Message:', message.strip[]]

# Output: Message: Learn Python

Syntax of String strip[]

The syntax of the strip[] method is:

string.strip[[chars]]

strip[] Parameters

  • chars [optional] - a string specifying the set of characters to be removed from the left and right part of the string.

The strip[] method removes characters from both left and right based on the argument [a string specifying the set of characters to be removed].

Note: If the chars argument is not provided, all leading and trailing whitespaces are removed from the string.

strip[] Return Value

strip[] returns a copy of the string with both leading and trailing characters stripped.

Working of the strip[] method

  • When the character of the string in the left mismatches with all the characters in the chars argument, it stops removing the leading characters.
  • Similarly, when the character of the string in the right mismatches with all the characters in the chars argument, it stops removing the trailing characters.

Example: Working of the strip[] method

string = '  xoxo love xoxo   '

# Leading and trailing whitespaces are removed

print[string.strip[]]

# All ,x,o,e characters in the left # and right of string are removed

print[string.strip[' xoe']]

# Argument doesn't contain space # No characters are removed. print[string.strip['stx']] string = 'android is awesome'

print[string.strip['an']]

Output

xoxo love xoxo
lov
  xoxo love xoxo   
droid is awesome

Here, we can see that the first expression string.strip[] without any arguments removed the whitespaces from the left and right of string.

  • string.strip[' xoe'] - Removes all whitespace, x, o, and e that lead or trailed the string.
  • string.strip['stx'] - Since string has whitespace at the beginning and end, this expression does not change the string. x is not removed since it is at the middle of the string [whitespaces lead and trail the string]
  • string.strip['an'] - Removes an leading the string.

❮ String Methods

Example

Remove spaces at the beginning and at the end of the string:

txt = "     banana     "

x = txt.strip[]

print["of all fruits", x, "is my favorite"]

Try it Yourself »

Definition and Usage

The strip[] method removes any leading [spaces at the beginning] and trailing [spaces at the end] characters [space is the default leading character to remove]

Syntax

Parameter Values

ParameterDescription
characters Optional. A set of characters to remove as leading/trailing characters

More Examples

Example

Remove the leading and trailing characters:

txt = ",,,,,rrttgg.....banana....rrr"

x = txt.strip[",.grt"]

print[x]

Try it Yourself »

❮ String Methods


I can strip numerics but not alpha characters:

>>> text
'132abcd13232111'

>>> text.strip['123']
'abcd'

Why the following is not working?

>>> text.strip['abcd']
'132abcd13232111'

asked Apr 4, 2017 at 17:53

2

The reason is simple and stated in the documentation of strip:

str.strip[[chars]]

Return a copy of the string with the leading and trailing characters removed. 
The chars argument is a string specifying the set of characters to be removed.

'abcd' is neither leading nor trailing in the string '132abcd13232111' so it isn't stripped.

answered Apr 4, 2017 at 17:56

1

Just to add a few examples to Jim's answer, according to .strip[] docs:

  • Return a copy of the string with the leading and trailing characters removed.
  • The chars argument is a string specifying the set of characters to be removed.
  • If omitted or None, the chars argument defaults to removing whitespace.
  • The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped.

So it doesn't matter if it's a digit or not, the main reason your second code didn't worked as you expected, is because the term "abcd" was located in the middle of the string.

Example1:

s = '132abcd13232111'
print[s.strip['123']]
print[s.strip['abcd']]

Output:

abcd
132abcd13232111

Example2:

t = 'abcd12312313abcd'
print[t.strip['123']]
print[t.strip['abcd']]

Output:

abcd12312313abcd
12312313

answered Apr 4, 2017 at 18:04

dot.Pydot.Py

4,8814 gold badges28 silver badges50 bronze badges

0

What is text Strip [] in Python?

Python String strip[] The strip[] method returns a copy of the string by removing both the leading and the trailing characters [based on the string argument passed].

What is text Strip?

The Text strip allows you to directly display text in the Sequence editor.

How do you strip a text file in Python?

Use str. rstrip or str. lstrip to strip space from right or left end only. All of these methods return new strings.

Chủ Đề