In this source code example, we will demonstrate how to use the Python string strip() method to return a copy of a string with the leading and trailing characters removed.
Use the Python string strip(chars) method to return a copy of a string with the leading and trailing chars removed.
The following example uses the strip() method to return a copy of a string with the leading and trailing characters . and # removed:
Python
Python String
Python string strip() method example
The following example uses the strip() method to return the copy of a string with the leading and trailing whitespace characters removed:s = '\tHi, how are you?\n '
print(s)
new_s = s.strip()
print(new_s)
Output:
Hi, how are you?
Hi, how are you?
heading = '#.....Section 1.2.3 Bug #45.....'
new_heading = heading.strip('.#')
print(new_heading)
Output:
Section 1.2.3 Bug #45
Related Python String Examples
- Python string literals
- Python string length example
- Python String join() method example
- Python string split() method example
- Python String index() method example
- Python string find() method example
- Python string startswith() method example
- Python string endswith() method example
- Python String lower() method example
- Python String upper() method example
- Python string title() method example
- Python string capitalize() method example
- Python string islower() method example
- Python string istitle() method example
- Python string isupper() method example
- Python string swapcase() method example
- Python string strip() method example
- Python string replace() method example
- Python string isdigit() method example
- Python string isdecimal() method example
- Python string isnumeric() method example
- Python string isalpha() method example
- Python string isalnum() method example
Comments
Post a Comment