Python String strip() - Programiz
文章推薦指數: 80 %
The strip() method removes characters from both left and right based on the argument (a string specifying the set of characters to be removed). Note: If the ...
CourseIndex
ExploreProgramiz
Python
JavaScript
SQL
C
C++
Java
Kotlin
Swift
C#
DSA
LearnPythonpractically
andGetCertified.
ENROLL
PopularTutorials
GettingStartedWithPython
PythonifStatement
whileLoopinPython
PythonLists
DictionariesinPython
StartLearningPython
PopularExamples
Addtwonumbers
Checkprimenumber
Findthefactorialofanumber
PrinttheFibonaccisequence
Checkleapyear
ExplorePythonExamples
ReferenceMaterials
Built-inFunctions
ListMethods
DictionaryMethods
StringMethods
Viewall
LearningPaths
Challenges
LearnPythonInteractively
TryforFree
Courses
BecomeaPythonMaster
BecomeaCMaster
BecomeaJavaMaster
ViewallCourses
Python
JavaScript
SQL
C
C++
Java
Kotlin
Swift
C#
DSA
LearnPythonpractically
andGetCertified.
ENROLLFORFREE!
PopularTutorials
GettingStartedWithPython
PythonifStatement
whileLoopinPython
PythonLists
DictionariesinPython
StartLearningPython
AllPythonTutorials
ReferenceMaterials
Built-inFunctions
ListMethods
DictionaryMethods
StringMethods
Viewall
Python
JavaScript
C
C++
Java
Kotlin
LearnPythonpractically
andGetCertified.
ENROLLFORFREE!
PopularExamples
Addtwonumbers
Checkprimenumber
Findthefactorialofanumber
PrinttheFibonaccisequence
Checkleapyear
AllPythonExamples
PythonStringMethods
PythonStringcapitalize()
PythonStringcenter()
PythonStringcasefold()
PythonStringcount()
PythonStringendswith()
PythonStringexpandtabs()
PythonStringencode()
PythonStringfind()
PythonStringformat()
PythonStringindex()
PythonStringisalnum()
PythonStringisalpha()
PythonStringisdecimal()
PythonStringisdigit()
PythonStringisidentifier()
PythonStringislower()
PythonStringisnumeric()
PythonStringisprintable()
PythonStringisspace()
PythonStringistitle()
PythonStringisupper()
PythonStringjoin()
PythonStringljust()
PythonStringrjust()
PythonStringlower()
PythonStringupper()
PythonStringswapcase()
PythonStringlstrip()
PythonStringrstrip()
PythonStringstrip()
PythonStringpartition()
PythonStringmaketrans()
PythonStringrpartition()
PythonStringtranslate()
PythonStringreplace()
PythonStringrfind()
PythonStringrindex()
PythonStringsplit()
PythonStringrsplit()
PythonStringsplitlines()
PythonStringstartswith()
PythonStringtitle()
PythonStringzfill()
PythonStringformat_map()
RelatedTopics
PythonStringlstrip()
PythonStringrstrip()
PythonStringisspace()
PythonStringexpandtabs()
Pythonfloat()
PythonStringcenter()
PythonStringstrip()
Inthistutorial,wewilllearnaboutthePythonStringstrip()methodwiththehelpofexamples.
Thestrip()methodreturnsacopyofthestringbyremovingboththeleadingandthetrailingcharacters(basedonthestringargumentpassed).
Example
message='LearnPython'
#removeleadingandtrailingwhitespaces
print('Message:',message.strip())
#Output:Message:LearnPython
SyntaxofStringstrip()
Thesyntaxofthestrip()methodis:
string.strip([chars])
strip()Parameters
chars(optional)-astringspecifyingthesetofcharacterstoberemovedfromtheleftandrightpartofthestring.
Thestrip()methodremovescharactersfrombothleftandrightbasedontheargument(astringspecifyingthesetofcharacterstoberemoved).
Note:Ifthecharsargumentisnotprovided,allleadingandtrailingwhitespacesareremovedfromthestring.
strip()ReturnValue
strip()returnsacopyofthestringwithbothleadingandtrailingcharactersstripped.
Workingofthestrip()method
Whenthecharacterofthestringintheleftmismatcheswithallthecharactersinthecharsargument,itstopsremovingtheleadingcharacters.
Similarly,whenthecharacterofthestringintherightmismatcheswithallthecharactersinthecharsargument,itstopsremovingthetrailingcharacters.
Example:Workingofthestrip()method
string='xoxolovexoxo'
#Leadingandtrailingwhitespacesareremoved
print(string.strip())
#All
延伸文章資訊
- 1Python String strip() - Programiz
The strip() method removes characters from both left and right based on the argument (a string sp...
- 2Python string | strip() - GeeksforGeeks
strip() is an inbuilt function in Python programming language that returns a copy of the string w...
- 3Python 字符串strip() 使用方法及示例 - 菜鸟教程
Python 字符串方法strip()方法返回字符串的副本,用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。注意:该方法只能删除开头或是结尾的字符, ...
- 4python中的strip()函式的用法 - 程式人生
python中的strip()函式的用法 ... 它的函式原型:string.strip(s[, chars]),它返回的是字串的副本,並刪除前導和字尾字元。 (意思就是你想去掉字串裡面的 ...
- 5Python strip()方法- Python教學 - 極客書
strip()方法返回所有字符從開始及字符串的末尾(默認空格字符)被去除後的字符串的一個副本。 語法以下是strip()方法的語法: str . strip ([ chars ]) ...