In this tutorial, we will look at how to split a string into a list of strings on the occurrences of underscore in Python with the help of examples.
How to split a string in Python?
You can use the Python string
split() function to split a string (by a delimiter) into a list of strings. To split a string by underscore in Python, pass the underscore character
"_" as a delimiter to the
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
The following is the syntax –
# split string s by underscore s.split("_")
It returns a list of strings resulting from splitting the original string on the occurrences of
Let’s look at some examples.
Split string by underscore
Here, we pass underscore as the delimiter to the string
# string with underscores s = "you_have_the_right_to_remain_silent" # split string by underscore ls = s.split("_") print(ls)
['you', 'have', 'the', 'right', 'to', 'remain', 'silent']
The resulting list contains words resulting from the split of the original string by underscore.
Fix the number of splits
You can also specify the maximum number of splits to be made using the
maxsplit parameter. By default, the string
split() function makes all the possible splits.
Let’s only split the above string into two parts at the occurrence of underscore,
"_"starting from the left. To split the string into two parts, the
maxsplit should be
1, because we’re making only one split resulting in two strings.
# string with underscores s = "you_have_the_right_to_remain_silent" # split string by underscore ls = s.split("_", maxsplit=1) print(ls)
You can see that the resulting list has only two strings.
Let’s look at another example.
Let’s split the original string into three parts, here we pass
# string with underscores s = "you_have_the_right_to_remain_silent" # split string by underscore ls = s.split("_", maxsplit=2) print(ls)
['you', 'have', 'the_right_to_remain_silent']
The resulting list has only three strings.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.