

The regular expression that represents one or more new line characters is \n+. We will use re package to split the string with one or more new line characters as delimiter. In this example, we will take a string with substrings separated by one or more new line characters. Output Example 3: Split String by One or More New Lines To use re package, we have to import it at the start of our program. In this example, we will split the string by new line using split() method of regular expression re package. Python Program str = 'Welcome\nto\'Įxample 2: Split String by New Line using re.split() The string can also contain \n characters in the string as shown below, instead of a multi-line string with triple quotes. split() method splits the string by new line character and returns a list of strings. We will call the split() method on this string with new line character \n passed as argument. In this example, we will take a multiline string string1. Example 1: Split String by New Line using str.split() In this tutorial, we will learn how to split a string by new line character \n in Python using str.split() and re.split() methods. You can split a string in Python with new line as delimiter in many ways. Example 3: Split String by One or More New Lines.Example 2: Split String by New Line using re.split().Example 1: Split String by New Line using str.split().Python NameError: name 'string' is not defined.Python - Check if String contains only Alphanumeric Characters.Python String Capitalize First Character.Python String - Find the number of overlapping occurrences of a substring.Python String - Find the index of first occurrence of substring.Python String - Replace character at Specific Position.Python RegEx - Find numbers of specific length in String.


Python Generate Random String of Specific Length.Python Check if the String contains only Alphabets.Python Strip() - Remove White Spaces at Start and End of String.Python Print String to Console Output Example.
