Remove leading and trailing spaces?

后端 未结 4 1430
醉梦人生
醉梦人生 2020-12-07 19:57

I\'m having a hard time trying to use .strip with the following line of code.

Thanks for the help.

f.write(re.split(\"Tech ID:|Name:|Account #:\",li         


        
4条回答
  •  青春惊慌失措
    2020-12-07 20:23

    Should be noted that strip() method would trim any leading and trailing whitespace characters from the string (if there is no passed-in argument). If you want to trim space character(s), while keeping the others (like newline), this answer might be helpful:

    sample = '  some string\n'
    sample_modified = sample.strip(' ')
    
    print(sample_modified)  # will print 'some string\n'
    

    strip([chars]): You can pass in optional characters to strip([chars]) method. Python will look for occurrences of these characters and trim the given string accordingly.

提交回复
热议问题