Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does s.strip() do exactly?

Tags:

python

I was told it deletes whitespace but

s = "ss asdas vsadsafas asfasasgas"

print(s.strip())

prints out

ss asdas vsadsafas asfasasgas

shouldn't it be ssasdasvsadsafasasfasasgas?

like image 855
Eric Jung Avatar asked Dec 09 '12 01:12

Eric Jung


People also ask

What is Strip () in python?

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)

What does Strip do in C?

STRIP. returns string with leading or trailing characters or both removed, based on the option you specify.

What do you mean by strip ()? What are the different types of strip () functions?

Python strip functions remove characters or white spaces from the beginning or end of a string. The strip functions are strip(), lstrip(), and rstrip(). They remove characters from both ends of a string, the beginning only, and the end only. Python strip() can also remove quotes from the ends of a string.

How do you remove trailing spaces in python?

strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.


2 Answers

You should perhaps check out the docs for the function. It trims leading and trailing whitespace. " ss ss ".strip() becomes "ss ss"

Relevant link: http://docs.python.org/2/library/stdtypes.html#str.strip

Additionally, there is a very powerful tool within the Python interactive interpreter - you can do something like help("".strip) to get a bevy of useful information.

like image 140
Santiclause Avatar answered Sep 20 '22 20:09

Santiclause


Definition from Python's reference:

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. 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:

like image 35
alinsoar Avatar answered Sep 21 '22 20:09

alinsoar