Whitespace refers to any of the following characters:
The strip
method is used to remove any of these characters, whether leading or trailing, from a string.
str.strip
where str
is the string we want to remove whitespace characters from.
This method doesn’t take any parameters.
This method returns a string str
but with all whitespace characters removed.
# create some stringsstr1 = "\tEdpresso"str2 = "\n\f\vis"str3 = " awesome "# remove whitespacea = str1.stripb = str2.stripc = str3.strip# print resultsputs aputs bputs c
Lines 2-4: We have strings that contain whitespace characters.
Lines 7-9: We use the strip
method to remove any whitespace characters and store the results in variables a
, b
, and c
.
Lines 12-14: We print our results to the console.