![]() |
|
[Tut] How to Split a Multi-line String into Multiple Lines? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Split a Multi-line String into Multiple Lines? (/thread-100389.html) |
[Tut] How to Split a Multi-line String into Multiple Lines? - xSicKxBot - 12-12-2022 How to Split a Multi-line String into Multiple Lines? Rate this post Summary: Use Code: given_string.splitlines()Minimal Example: Code: text = 'Python\nJava\nC#'
print(text.splitlines())
# Output: ['Python', 'Java', 'C#'] Problem: Given a string, How will you split the string into a list of words using newline as a separator/delimiter?Example: Code: # Input
text = """abc
def
ghi """
# Expected Output
['abc', 'def', 'ghi']Let’s dive into the different ways of solving the given problem. Method 1: Using splitlines Approach: The most convenient and easiest way to split a given multiline string into multiple strings is to use the Code: splitlines()Code: 'given_string'.splitlines()NOTE: Code: splitlines()Code: '\n'Code: 'finxter\nis\ncool'.splitlines()Code: ['finxter', 'is', 'cool']Code: Code: # Input
text = """Python is an Object Oriented programming language.
COBOL is an Object Oriented programming language.
F# is an Object Oriented programming language.""" print(text.splitlines()) # Output: ['Python is an Object Oriented programming language.', 'COBOL is an Object Oriented programming language.', 'F# is an Object Oriented programming language.'] Related Read: Python String splitlines()Method 2: Using split() Approach: Use Code: 'given_string'.split('\n')Code: Code: # Input
text = """Python is an Object Oriented programming language.
COBOL is an Object Oriented programming language.
F# is an Object Oriented programming language.""" print(text.split('\n')) # Output: ['Python is an Object Oriented programming language.', 'COBOL is an Object Oriented programming language.', 'F# is an Object Oriented programming language.']Using “\n” ensures that whenever a new line occurs, the string is split. Related Read: Python String split()Method 3: Using re.split in a List Comprehension Another way to solve the given problem is to use the split method of the regex module. You can split the string at every line break by passing “\n” as the pattern within the Code: re.splitCode: Code: import re text = """Python is an Object Oriented programming language.
COBOL is an Object Oriented programming language.
F# is an Object Oriented programming language.""" print([x for x in re.split("\n", text) if x!='']) # Output: ['Python is an Object Oriented programming language.', 'COBOL is an Object Oriented programming language.', 'F# is an Object Oriented programming language.']NOTE: The Code: re.split(pattern, string)Code: patternCode: stringCode: re.split('a', 'bbabbbab')Code: ['bb', 'bbb', 'b'] Read more here – Python Regex SplitList Comprehension: “A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.” Read more here: List Comprehension in Python — A Helpful Illustrated GuideDo you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video. Conclusion We have successfully solved the given problem using three different approaches. I hope you this article answered all your queries. Please subscribe and stay tuned for more interesting articles! Happy coding! ![]() Related Reads: ⦿ Python | Split String by Newline ⦿ Python | Split String by Whitespace⦿ Python | Split String into Characters Python Regex Course Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages. Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages. Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions. Regular expressions rule the game when text processing meets computer science. If you want to become a regular expression master too, check out themost comprehensive Python regex course on the planet:
|