How to remove the first character of a string in Python:
In this post, we will learn how to remove the first character of a string in Python. I will show you different ways to do it in this post.
Method 1: By using string slicing:
String slicing is the easiest way to remove the first string character in Python. We can slice a string like slice[start, end]. It will return a string from index start to end - 1. If we don’t pass the end index, it will create a substring from start to the end.
So, if we pass 1 as the starting index, it will return a sub-string from index 1 to the end of the original string i.e. it will remove the first character of the string and create a new string.
Let’s take a look at the below program:
given_str = 'Hello World'
new_str = given_str[1:]
print('New string: ', new_str)
Here, given_str is the given string. We are using string slicing to remove the first character from this string and the newly created string is assigned to the string variable new_str.
If you run this program, it will print:
New string: ello World
As you can see here, the first character of the string is removed by slicing.
Method 2: By using split:
We can use the split method to get the sub-string starting from the second index to the end. For that, we will use the split method to split the string in two parts at the first occurrence of the first character.
given_str.split(given_str[0],1)
It will return one list of words using the first character of the string as its delimiter. The second parameter defines the number of splits to do.
For example, if the string is AAHELLO WORLD, it will return [”, ‘AHELLO WORLD’].
Now, we can pick the second string as that is the required string. Below is the complete program:
given_str = 'AAHELLO WORLD'
new_str = given_str.split(given_str[0],1)[1]
print('New string: ', new_str)
It will print:
New string: AHELLO WORLD
Note that it might throw IndexError: string index out of range error if we try it with an empty string. We will have to add one check for that:
given_str = ''
if len(given_str) > 1:
new_str = given_str.split(given_str[0],1)[1]
print('New string: ', new_str)
You can add one else block to handle this scenario.
Method 3: By using replace:
The replace method is another way to remove the first character of a string. This method replaces an old string with a new string for a given number of occurrences. It is defined as:
str.replace(old, new, n)
- It will replace all old substrings by new in the string str.
- n is an optional value. It is the number of occurrences to replace.
It will return the new string.
We can pass the first character of the string as the old, and an empty string as the new and 1 as n. It will replace the first character with an empty string and return the required string.
given_str = 'hello world'
if len(given_str) > 1:
new_str = given_str.replace(given_str[0], "", 1)
print('New string: ', new_str)
It will give a similar result. Note that, we have to check for an empty string with this method as well.
You might also like:
- Python string isdigit() method explanation with example
- Python string isidentifier() method
- Python string islower method explanation with example
- Python program to check if a number is divisible by another number or not
- Python program to check if a number is duck number or not
- Python program to convert celsius to Fahrenheit and vice versa
- Python program to check if a number is a magic number or not
- Difference between random.sample() vs random.choice() in Python
- Python program to check for Moran numbers