🌐
W3Schools
w3schools.com β€Ί python β€Ί ref_string_split.asp
Python String split() Method
Remove List Duplicates Reverse a String Add Two Numbers Β· Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... The split() method splits a string into a list.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-string-split
Python split() Method - GeeksforGeeks
4 weeks ago - It returns the resulting parts as a list of strings. ... Explanation: s.split(',') splits the string s at every comma and returns a list of the parts ['one', 'two', 'three'].
Discussions

Explaining Split function
The split function is used over a string to separate it into smaller pieces. To do this, the function needs a string parameter with the separator that will determine how the string is split. Let's say that I have a string that contains a list of items separated by commas, like this: shop_string = "milk, soap, chocolate, cereals, bread, coffee, snacks" Now I want to recover this items and process them, but as they are in a string, I can't take each one separately and do whatever with them. I have to split the string to get each one of them. And then is when the split() functions come to save us. But after using it, we need to know which separator is more adequate to get what we need. In this case, it is obvious that we can use the commas as a separator to get each individual element, which will be returned as a list. So, we can use the function just like this: shop_list = shop_string.split(',') This will return a list in which each element is a string from the original shop_string, but removing the separator (in this case, the commas) and everything that comes after it. It's like the function searches through the string until it finds the separator, removes it and pushes to the list everything that it read until reaching the separator (as a string), and then does the same from the point it was (not from the start). So the contents of shop_list will be: ['milk', ' soap', ' chocolate', ' cereals', ' bread', ' coffee', ' snacks'] Remember that the commas now are separators for the elements of the list, they are not inside each element. You can see that each element, except the first, has also a leading space, as it was in the original string. So, if we want to get the elements without the space, we can add it to the separator and it will remove it for us, just like this: shop_list = shop_string(', ') ['milk', 'soap', 'chocolate', 'cereals', 'bread', 'coffee', 'snacks'] Be aware that if you use a separator that doesn't exist in the string, it will be return the string as a list and won't do anything: shop_list = shop_string('_') ['milk, soap, chocolate, cereals, bread, coffee, snacks'] This is it because it couldn't find the separator, so it searched in all the string, and returned what it read, that was everything. The function is really useful when you want to get certain parts of a string to use them later, as you can use multiple split to get the exact part that you need. For example, we have the following string: conf = "Configuration values: min_str_len=5; max_str_len=10; min_int_value=0; max_int_value=100 This string contains some configuration values, and we need to operate with them later, but as they're in a string, is really difficult to get the exact name and value of each configuration. So we can use split to get name and value of each configuration. First, we can remove the leading part of the string, that isn't relevant: conf_values = conf.split(': ')[1] The result of this line will be: 'min_str_len=5; max_str_len=10; min_int_value=0; max_int_value=100' Because split with the separator ': ' returns a list with two elements: the trailing statement before the colon, and the rest of the string that contains the values that we want to recover, so we select only the second part using [1]. Now we have to separate each configuration, and we can do it using the semicolon and the space as separator: conf_values = conf_values.split('; ') ['min_str_len=5', 'max_str_len=10', 'min_int_value=0', 'max_int_value=100'] Now we have a list with each configuration, but still we have to get the actual name and value of each element, so we can iterate through the list and use split again on each one, using = as the separator: for configuration in conf_values: name, value = configuration.split('=') dict.update({name:int(value)}) In the first line, we iterate over the list using a variable called configuration that will hold the value of the element on each iteration. Then, we can use split on it to get the values that we want. As we know that the use of this split will return a list with only two elements, we can use two variables and assign them directly, named name and value, holding each one the name and value of the configuration, respectively. After this, we can use them for whatever we want, but as we are in a loop, we will lose the actual content of both values after it finishes, so we can store them somewhere, for example in a dictionary using the update function. We can also store the value as an int if we want. The result of the dictionary will be: {'min_str_len':5, 'max_str_len':10, 'min_int_value':0, 'max_int_value':100} I hope that now you understand a little better how the split function works! More on reddit.com
🌐 r/learnpython
2
2
November 25, 2021
How to quickly reformat very long strings in python code using elpy+flycheck while maintaining PEP8-compliance?

Hey, this is not a direct answer to your question but I'd like to point out a different python package that might help you with python formatting https://github.com/psf/black

Black is an autoformatter that will modify all of your python code so that it looks consistent. It is very opinionated about how your code should look so that you don't have to be. I think it gets the formatting right most of the time and in the cases I disagree I just let it do its thing because its still consistent. Using black has allowed me to stop worrying about formatting entirely. There is also an emacs package to interface with it: https://github.com/pythonic-emacs/blacken

It sadly does not wrap and split long strings and comment, though it will wrap and split expressions

More on reddit.com
🌐 r/emacs
7
5
September 25, 2019
string split not working with newline (`\n`)
That article isn't great. The first problem is communicate returns a tuple. https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate communicate() returns a tuple (stdout_data, stderr_data) So str(data.communicate()) is "wrong". The second problem is stdout_data is of type bytes not str - and calling str() is also "wrong". The "newer" way of doing this would be: cmd = subprocess.run(['ls', '-l'], capture_output=True, text=True) for line in cmd.stdout.splitlines(): print('Line is:', line) Which would be somewhat equivalent to: cmd = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE) stdout, stderr = cmd.communicate() for line in stdout.decode().splitlines(): print('Line is:', line) More on reddit.com
🌐 r/learnpython
6
1
February 9, 2021
"AttributeError: 'list' object has no attribute 'split'"
I'm not completely sure what you want, but perhaps try file.read () instead of file.readlines() if you want the text as a string instead of a list. More on reddit.com
🌐 r/learnpython
19
2
April 22, 2021
🌐
Programiz
programiz.com β€Ί python-programming β€Ί methods β€Ί string β€Ί split
Python String split()
grocery.split(', ') - splits the string into a list of substrings at each comma and space character.
🌐
Mimo
mimo.org β€Ί glossary β€Ί python β€Ί string-split-method
Learn Python's String Split Method, Simplify Text Processing
Start your coding journey with Python. Learn basics, data types, control flow, and more ... 1. Split by Whitespace (Default): If you call .split() with no arguments, it splits the string by any sequence of whitespace (spaces, tabs, newlines) and discards empty strings.
🌐
Tutorialspoint
tutorialspoint.com β€Ί python β€Ί string_split.htm
Python String split() Method
str = "123.748289"; print("Separating ... In the example below, we are creating a string with the value: "aaa,,ccc,ddd,eee" and, called the split() method on it with comma (",") as as argument....
🌐
Hyperskill
hyperskill.org β€Ί university β€Ί python β€Ί split-in-python
Python split(): Split Strings by Delimiter with Examples
June 5, 2026 - The delimiter can be any character or substring that you want to use as a separator. The function searches for occurrences of the delimiter within the string and breaks it down at each occurrence. # Using whitespace as a delimiter text = "Hello World! Welcome to Python!" result = text.split() ...
🌐
Real Python
realpython.com β€Ί python-split-string
How to Split a String in Python – Real Python
November 4, 2024 - By passing a comma (",") as the argument to sep, you instruct Python to use commas as the delimiter for splitting. As a result, .split() separates the string into a list of individual names. Note that .split() doesn’t include the delimiter in the output. To consider another example, imagine that you have a line from a CSV file that contains product information:
🌐
Python Examples
pythonexamples.org β€Ί python-split-string
Python - Split String - 3 Examples
The result of the split operation is stored in the variable values_list, which will contain the following list: ['apple', 'banana', 'cherry', 'mango', 'fig']. The print(values_list) statement outputs the resulting list to the console, displaying: ['apple', 'banana', 'cherry', 'mango', 'fig']. ...
Find elsewhere
🌐
Great Learning
mygreatlearning.com β€Ί blog β€Ί it/software development β€Ί python string split() method
Python String split() Method
June 28, 2023 - By default, it splits the string by whitespace, but you can specify any delimiter, such as a comma, semicolon, or custom character. ... The method returns a list of substrings. ... In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions.
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί how-to-split-a-string-in-python
Python .split() – Splitting a String in Python
September 8, 2022 - In the first example of the previous section, .split() split the string each and every time it encountered the separator until it reached the end of the string.
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί python-split-string-how-to-split-a-string-into-a-list-or-array-in-python
Python Split String – How to Split a String into a List or Array in Python
April 4, 2023 - In this example, we split the string "hello\nworld" into a list of two elements, "hello" and "world", using the splitlines() method. The re module in Python provides a powerful way to split strings based on regular expressions.
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί python-split-string-splitting-example
Python split() – String Splitting Example
May 11, 2022 - In this section, we'll see some examples of string splitting using the split() method without passing in any parameters. myString = "Python is a programming language" print(myString.split()) # ['Python', 'is', 'a', 'programming', 'language']
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python-string-split
Python String split() - GeeksforGeeks
April 2, 2025 - In this article, we'll look at different ways to split and parse strings in Python. Let's understand this with the help of a basic example:Pythons = "geeks,for,geeks" # Split the string by commas res = s.split(',') # Parse the list and print each element for item in res: print(item)Outputgeeks for g
🌐
Software Testing Help
softwaretestinghelp.com β€Ί home β€Ί python programming for beginners – free python tutorials β€Ί python string split tutorial
Python String Split Tutorial
April 1, 2025 - Python split() method is used to split the string into chunks, and it accepts one argument called separator. A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default. ... In the above example, we have used the split() function to split the string without any arguments.
🌐
Simplilearn
simplilearn.com β€Ί home β€Ί resources β€Ί software development β€Ί python split() function: how to use split() in python
Python split() Function: Syntax, and Usage Guide
July 31, 2025 - The split() function in Python can be used to split a given string or a line by specifying one of the substrings of the given string as the delimiter.
Address Β  5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Note.nkmk.me
note.nkmk.me β€Ί home β€Ί python
Split a String in Python (Delimiter, Line Breaks, Regex) | note.nkmk.me
May 4, 2025 - This article explains how to split strings in Python using delimiters, line breaks, regular expressions, or a number of characters. Split a string by delimiter: split()Specify the delimiter: sepLimit ...
🌐
Upgrad
upgrad.com β€Ί home β€Ί blog β€Ί data science β€Ί python split() function: syntax, parameters, examples
Python Split() Function: Examples, Methods & Practical Tips
3 weeks ago - If you only need to split a string into three parts based on the first or last occurrence of a separator, partition() is a great choice. It always returns a tuple of three elements: The part before the separator. The separator itself. The part after the separator. Python email = "contact@example.com" parts = email.partition('@') print(parts)
🌐
PythonForBeginners
pythonforbeginners.com β€Ί home β€Ί how to use split in python
How to use Split in Python - PythonForBeginners.com
January 30, 2021 - Quick Example: How to use the split function in python ... x.split(β€œ,”) – the comma is used as a separator. This will split the string into a string array when it finds a comma.
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί python-string-split-and-join-methods-explained-with-examples
Python String split() and join() Methods – Explained with Examples
October 18, 2021 - The split() method acts on a string and returns a list of substrings. The syntax is: ... For example, if you'd like to split <string> on the occurrence of a comma, you can set sep = ",".
🌐
Learn By Example
learnbyexample.org β€Ί python-string-split-method
Python String split() Method - Learn By Example
April 20, 2020 - By default, split() will make all possible splits (there is no limit on the number of splits). When you specify maxsplit, however, only the given number of splits will be made. ... When delimiter is not specified, the string is split on whitespace.