19
loading...
This website collects cookies to deliver better user experience
lstrip()
, rstrip()
, and strip()
, respectively.lstrip()
method.<string>.lstrip()
returns a copy of the string with all leading whitespaces removed.
l
in lstrip()
stands for left. So this method removes all whitespaces to the left of the first character in the string." Coding is hard, yet fun!\t"
- it has 4 leading whitespaces, and 1 trailing tab (\t
).lstrip()
method on this string. And store the returned string in the variable l_str
.long_str = " Coding is hard, yet fun!\t"
l_str = long_str.lstrip()
long_str
, and the returned string l_str
.print(len(long_str))
print(len(l_str))
# Output
29
25
rstrip()
method.<string>.rstrip()
returns a copy of the string with all trailing whitespaces removed.
r
in rstrip()
stands for right. So this method removes all whitespaces to the right of the last character in the string.rstrip()
method on this string, store the returned string in the variable r_str
.long_str = " Coding is hard, yet fun!\t"
r_str = long_str.rstrip()
print(len(long_str))
print(len(r_str))
# Output
29
28
\t
has been removed. And the returned string's length is one less than the original string, as expected.lstrip()
and rstrip()
respectively, in this section, you'll learn how strip()
method helps remove both leading and trailing whitespaces.<string>.strip()
returns a copy of the string with all leading and trailing whitespaces removed.
strip()
method on the string long_str
.long_str = " Coding is hard, yet fun!\t"
s_str = long_str.strip()
print(len(long_str))
print(len(s_str))
# Output
29
24
\t
have been removed.Method | Description |
---|---|
lstrip() |
Returns a copy of the string with all leading whitespaces removed |
rstrip() |
Returns a copy of the string with all trailing whitespaces removed |
strip() |
Returns a copy of the string with all leading and trailing whitespaces removed |