How do I split up a string into several parts of a number of words in python. For example, turn a 10,000 word string into ten 1,000 word strings. Thanks.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Where n is number of words; s is the long string.
This will yield ten 1000 word strings from a 10000 word string like you ask.
Note that you can also use iterools grouper recipe but that would involve making 1000 copies of the iterator for your string: expensive I think.
Also note that this will replace all whitespace with spaces. If this isn’t acceptable, you’ll need to try something else.