Python String strip() - Programiz

文章推薦指數: 80 %
投票人數:10人

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,x,o,echaractersintheleft #andrightofstringareremoved print(string.strip('xoe')) #Argumentdoesn'tcontainspace #Nocharactersareremoved. print(string.strip('stx')) string='androidisawesome' print(string.strip('an')) Output xoxolovexoxo lov xoxolovexoxo droidisawesome Here,wecanseethatthefirstexpressionstring.strip()withoutanyargumentsremovedthewhitespacesfromtheleftandrightofstring. string.strip('xoe')-Removesallwhitespace,x,o,andethatleadortrailedthestring. string.strip('stx')-Sincestringhaswhitespaceatthebeginningandend,thisexpressiondoesnotchangethestring.xisnotremovedsinceitisatthemiddleofthestring(whitespacesleadandtrailthestring) string.strip('an')-Removesanleadingthestring. PreviousTutorial: PythonStringrstrip() NextTutorial: PythonStringpartition() Shareon: Didyoufindthisarticlehelpful? Sorryaboutthat. Howcanweimproveit? Feedback* Leavethisfieldblank PythonReferencesPythonLibraryPythonStringlstrip()PythonLibraryPythonStringrstrip()PythonLibraryPythonStringisspace()PythonLibraryPythonStringexpandtabs() TryPROforFREE LearnPythonInteractively



請為這篇文章評分?