YouTip LogoYouTip

Python3 String Strip

# Python3.x Python3 strip() Method [![Image 3: Python3 Strings](#) Python3 Strings](#) * * * ## Description The Python `strip()` method is used to remove specified characters (default is whitespace) or a sequence of characters from the beginning and end of a string. **Note:** This method can only remove characters from the beginning or the end, not from the middle of the string. ## Syntax The syntax for the `strip()` method is: str.strip(); ## Parameters * chars -- A sequence of characters to be removed from the beginning and end of the string. ## Return Value Returns a new string with the specified character sequence removed from the beginning and end. ## Example The following examples demonstrate the usage of the `strip()` function: ## Example (Python 3.0+) #!/usr/bin/python3 str = " *****this is **string** example....wow!!!***** "print(str.strip('*'))# Specified string * The output of the above example is: this is **string** example....wow!!! As seen from the result, the characters in the middle part were not removed. The following example demonstrates that characters are removed as long as they are part of the specified character sequence at the beginning or end: ## Example (Python 3.0+) #!/usr/bin/python3 str = "123abctutorial321"print(str.strip('12'))# Character sequence is 12 The output of the above example is: 3abctutorial3 * * Python3 Strings](#)
← Python3 String StripPython3 String Replace β†’