Python :Tách từng từ trong chuỗi và liệt kê những từ theo yêu cầu

 

# the speech from "As You Like It"

speech = "All the world’s a stage,And all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts, His acts being seven ages. At first the infant,Mewling and puking in the nurse’s arms; And then the whining school-boy, with his satchel And shining morning face, creeping like snail Unwillingly to school. And then the lover, Sighing like furnace, with a woeful ballad Made to his mistress’ eyebrow. Then a soldier, Full of strange oaths, and bearded like the pard, Jealous in honour, sudden and quick in quarrel, Seeking the bubble reputation Even in the cannon’s mouth. And then the justice, In fair round belly with good capon lin’d,With eyes severe and beard of formal cut,Full of wise saws and modern instances;And so he plays his part. The sixth age shifts Into the lean and slipper’d pantaloon, With spectacles on nose and pouch on side; His youthful hose, well sav’d, a world too wide For his shrunk shank; and his big manly voice, Turning again toward childish treble, pipes And whistles in his sound. Last scene of all, That ends this strange eventful history, Is second childishness and mere oblivion; Sans teeth, sans eyes, sans taste, sans everything."

# split this into words

words = speech.split() =tách từng từ, mặc định dựa theo space, xuống dòng

# loop over all the words finding those with an X in

for word in words:

    revised_word = word.upper() chuyển hết thành upper/hoăch lower để tránh lỗi

    if "X" in revised_word:

        print(word)

Kiểm tra xem có chữ "X" trong từ revised_word không, nếu có, thì in ra từ gốc (word).

# loop over all the words finding those with a J in

for word in words:

    revised_word = word.upper()

    if "J" in revised_word:

        print(word)

Kiểm tra xem có chữ "J" trong từ revised_word không, nếu có, thì in ra từ gốc (word).

Kết quả:
exits
sixth
Jealous
justice,




Nhận xét