68 of 151 menu

The strip method

The strip method returns a string with the specified characters removed from the beginning and end. In the optional parameter of the method, we specify the characters we want to remove. If they are not specified, the method will only remove spaces from the beginning and end of the string.

Syntax

string.strip([characters to delete])

Example

Let's apply the strip method to the following line:

txt = 'abcdea' print(txt.strip('a'))

Result of code execution:

'bcde'

Example

Let's put spaces at the beginning and end of the string and apply the strip method again:

txt = ' abcdea ' print(txt.strip('a'))

Since the first and last characters of the string are now spaces, rather than the specified 'a', the method, finding no matches, simply returned the original string:

' abcdea '

Example

Now let's apply the strip method without specifying the parameter:

txt = ' abcdea ' print(txt.strip())

Result of code execution:

'abcdea'

See also

  • method rstrip,
    which removes characters from the end of a line
  • method lstrip,
    which removes characters from the beginning of a line
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline