3.5 การตัดคำ
>>> import
string
>>>
sentence = " This is a bad
sentence "
>>>
paragraph = "\t\This paragraph has
\n\
even more
problems!.? "
>>>
print "Length of sentence : " + str(len(sentence))
Length of sentence : 25
>>>
print "Length of sentence : " + str(len(sentence))
Length of sentence : 25
>>> print
"Length of sentence without space : " + str(len(sentence.strip("
")))
Length of sentence without space : 21
>>> print "Length of sentence without space (right) : " + str(len(sentence.rstrip(" ")))
Length of sentence without space (right) : 23
>>> print "Length of sentence without space (left) : " + str(len(sentence.lstrip(" ")))
Length of sentence without space (left) : 23
>>> print "Length of paragraph with tab : " + str(len(paragraph))
Length of paragraph with tab : 47
>>> print "Length of paragraph with tab and space: " + str(len(paragraph))
Length of paragraph with tab and space: 47
>>> print "Length of paragraph without space: " + str(len(paragraph.strip(" ")))
Length of paragraph without space: 44
>>> print "Length of paragraph without tab: " + str(len(paragraph.strip("\t")))
Length of paragraph without tab: 46
|
ภาพที่ 3.18 คำสั่งและผลลัพธ์การตัดคำ
ไม่มีความคิดเห็น:
แสดงความคิดเห็น