How to Splitting strings in Python without split()?
The easiest way to split a string in python using the Python split() function. But in this tutorial, we will find other ways (without using the split() function) to do it.
Do split strings, using a for loop, array and append:-
In the example, first need 2 variables, a “split_value” array variable for store splitter string and another one “
Inside for-loop condition statement have an if-else condition which checking the next character is space or not. If space then appends it in
sentence = 'Python Programming tutorial' split_value = [] tmp = '' for c in sentence: if c == ' ': split_value.append(tmp) tmp = '' else: tmp += c # for last word if tmp: split_value.append(tmp) print(split_value)
Output: [‘Python’, ‘Programming’, ‘tutorial’]
Output in Python List (Array for other programming languages) format? Use a loop to get in string format or get a single word by using indexing.
Do comment if you have any doubt and suggestion on this tutorial.
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6Python 3.7
All Examples of Splitting strings without function are in Python 3, so it may change its different from python 2 or upgraded versions.