2016-04-01 44 views
0

我有这个Python脚本,我没有得到如何.strip能够删除“/ n”。从文档中你必须提供.strip()字符以从字符串中删除,并返回字符串w.o它们。/n某种特殊情况是.strip()忽略并返回一个字符串w.o它?不理解Python String Strip()方法

from urllib import urlopen 

WORD_URL = "http://learncodethehardway.org/words.txt" 
WORDS = [] 

# load up the words from the website 
for word in urlopen(WORD_URL).readlines(): 
    WORDS.append(word.strip()) 

print "*" * 134 
print urlopen(WORD_URL).readlines() 

print "*" * 134 
print WORDS 

回答

4

.strip()未提供任何参数时,它将去除所有空白。包括换行符,回车符,制表符和空格。换行符是\n,因此包含它。从文档:

返回字符串的副本,删除前导字符和尾随字符。 chars参数是一个字符串,指定要删除的字符集。 如果省略或无,chars参数默认为删除空格。

(重点煤矿)

+0

真棒谢谢 –