In this tutorial, we will look at how to remove leading underscores from a string in Python with the help of some examples.
How to remove leading underscores from a string in Python?
You can use the string lstrip()
function to remove leading underscores from a string in Python. The lstrip()
function is used to remove characters from the start of the string and by default removes leading whitespace characters.
To remove leading underscores with the lstrip()
function, pass the underscore character, '_'
as an argument. Let’s look at an example.
# create a string s = "_not a doctor" # remove leading underscores s.lstrip('_')
Output:
'not a doctor'
The resulting string doesn’t have any underscore characters in the beginning.
Note that the lstrip()
function only removes characters from the start of the string. So, for example, if underscores are present in the middle, or at the end of a string, they will not be removed.
# create a string s = "_not_a_doctor_" # remove leading underscores s.lstrip('_')
Output:
'not_a_doctor_'
You can see that only the leading underscores were removed from the string.
Introductory ⭐
- 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
Intermediate ⭐⭐⭐
- 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
Advanced ⭐⭐⭐⭐⭐
- 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.
Similarly, you can use the string lstrip()
function to remove any character from the beginning of the string.
Remove all underscores
To remove all the occurrences of the underscore character irrespective of where it occurs in the string, you can use the string replace()
function. For example –
# create a string s = "_not_a_doctor_" # remove all underscores s.replace('_', '')
Output:
'notadoctor'
The resulting string does not have any underscore characters. Here we use the string replace()
function to replace all the occurrences of the underscore with an empty string. You can choose any character as a replacement to the underscore character depending upon your use case.
You might also be interested in –
- Remove First Character From String in Python
- Python – Remove Non Alphanumeric Characters from String
- Remove Substring From a String in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.