python/python 수업
fairytale slicing
by sj0020
2020. 9. 8.
>>> fairytale
'The Princess and the Pea\n\nOnce upon a time there was a prince who wanted to marry a princess; but she would have to be a real princess. He travelled all over the world to find one, but nowhere could he get what he wanted. There were princesses enough, but it was difficult to find out whether they were real ones. There was always something about them that was not as it should be. So he came home again and was sad, for he would have liked very much to have a real princess.\n One evening a terrible storm came on; there was thunder and lightning, and the rain poured down in torrents. Suddenly a knocking was heard at the city gate, and the old king went to open it.\n It was a princess standing out there in front of the gate. But, good gracious! what a sight the rain and the wind had made her look. The water ran down from her hair and clothes; it ran down into the toes of her shoes and out again at the heels. And yet she said that she was a real princess.\n Well, we\'ll soon find that out, thought the old queen. But she said nothing, went into the bed-room, took all the bedding off the bedstead, and laid a pea on the bottom; then she took twenty mattresses and laid them on the pea, and then twenty eider-down beds on top of the mattresses.\n On this the princess had to lie all night. In the morning she was asked how she had slept.\n "Oh, very badly!" said she. "I have scarcely closed my eyes all night. Heaven only knows what was in the bed, but I was lying on something hard, so that I am black and blue all over my body. It\'s horrible!"\n Now they knew that she was a real princess because she had felt the pea right through the twenty mattresses and the twenty eider-down beds.\n Nobody but a real princess could be as sensitive as that.\n So the prince took her for his wife, for now he knew that he had a real princess; and the pea was put in the museum, where it may still be seen, if no one has stolen it.\n There, that is a true story.\n'
>>> fairytale.find("Now")
1574
>>> slice = fairytale[1574:]
>>> slice
'Now they knew that she was a real princess because she had felt the pea right through the twenty mattresses and the twenty eider-down beds.\n Nobody but a real princess could be as sensitive as that.\n So the prince took her for his wife, for now he knew that he had a real princess; and the pea was put in the museum, where it may still be seen, if no one has stolen it.\n There, that is a true story.\n'
>>> slice.split('\n')
['Now they knew that she was a real princess because she had felt the pea right through the twenty mattresses and the twenty eider-down beds.', ' Nobody but a real princess could be as sensitive as that.', ' So the prince took her for his wife, for now he knew that he had a real princess; and the pea was put in the museum, where it may still be seen, if no one has stolen it.', ' There, that is a true story.', '']
>>> sts=slice.split('\n')
>>> sts
['Now they knew that she was a real princess because she had felt the pea right through the twenty mattresses and the twenty eider-down beds.', ' Nobody but a real princess could be as sensitive as that.', ' So the prince took her for his wife, for now he knew that he had a real princess; and the pea was put in the museum, where it may still be seen, if no one has stolen it.', ' There, that is a true story.', '']
>>> sts[0]
'Now they knew that she was a real princess because she had felt the pea right through the twenty mattresses and the twenty eider-down beds.'
>>>