remove first character from string

Remove First Character From String in Python

Python strings come with a number of useful methods and techniques to help you with string manipulation. In this tutorial, we will look at how to remove the first character from a string in Python with the help of some examples.

remove first character from string

The simplest way to remove the first character from a string is to slice the string from the second character to the end of the string. To slice a string, s from index i to index j (but not including the character at index j), you can use the notation s[i:j].

Strings (and other iterables) in Python are indexed starting from 0. This means that the first character in the string is present at the index 0 and the last character is present at the index len(s)-1. Since we want to include the last character we will slice till len(s). Let’s now use the two indices to slice the string.

# create a string
s = "Boomerang"
# remove the first character
s[1:len(s)]

Output:

'oomerang'

You can see that the resulting string has the first character from the original string removed. When slicing all the way to the end of the string, you don’t need to explicitly specify the end index in the slice.

# remove the first character
s[1:]

Output:

'oomerang'

We get the same result as above.

There are other methods as well to remove the first character from a string in Python. For example, you can use the string lstrip() function which by default removes the leading whitespace characters from a string but can be modified to remove any character from the beginning of a string. Pass the character or the characters to be removed from the start of the string as an argument.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

# create a string
s = "Boomerang"
# remove the first character
s.lstrip(s[0])

Output:

'oomerang'

We get the string with the first character removed. Here we pass the first character in s, s[0] as an argument to the lstrip() function to indicate that we want to remove this specific character from the start of the string.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top