How to remove the first character from a Python String
You will sometimes have the need to remove the first character from a Python string. For instance, consider the following piece of code:
The output will be:
We wish to remove the first character of the “price” string because it is the denomination of the price (i.e., dollars). After removing the first character we will be left with a string denoting a number which can be typecast using the float() function or can be processed in other ways. So how do we remove the first character of a Python string?
There are at least three ways. First, we can use string slicing. Alternatively, we can use the replace() method and replace the first character with a null string. Let us look at both of these approaches in turn! The third approach which is a bit convoluted, converts the string to a list, pops the first element out, and reconstructs the list back into a string.
Method 1: String slicing
In the first approach we slice the string. We use the square bracket slicing notation that takes as input starting and ending indices. In our cases the starting index is 1 (i.e., the second character) and there is quite no ending index (because we wish to retain the string till the end). So we can leave the specification of the ending index blank, like so:
The output is:
Method 2: Use the replace() method
In the second approach, we replace the first character with a null string:
The “replace” method takes as input the character to be replaced - in our case this is the first character, determined by price; and the character to replaced with - in our case this is the empty or null string. The output is as expected:
The main problem with this approach is that if the dollar symbol appears multiple times, it will replaced all times with the empty string. Thus, the above code does more than merely removing the first character of the string. For instance,
One way to get around this problem is to use a third argument that specifies the number of occurrences of the old value you wish to replace (the default is all occurrences). So if we give a third argument of “1”, we are all set:
Method 3: Use list operations
This third method is a bit complicated to write but is conceptually simple. We first convert the string into a list, then use the pop() method to remove the first element and then reconstruct the list into a string:
Note here the use of the list() constructor that constructs a list (called “listprice”) from the string (price). Then we apply the pop(0) method to remove the dollar sign. Finally, we use the join operation to reconstruct the string. The input to join takes each element of the list (listprice) and typecasts them to string (“str”) before joining. The initial value to the join is the empty string. The output is:
exactly as the other methods.
Thus, we have seen three methods to remove the first character from a Python string. Which one is your favorite?
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.