Last updated: Apr 9, 2024
Reading timeยท5 min
Use string slicing to remove the first N characters from a string, e.g.
new_string = string[N:]
.
The slice will remove the first N characters from the string by returning a copy of the string that starts at the character after the first N characters.
string = 'bobbyhadz.com' # โ Remove the first 2 characters from a string new_string = string[2:] print(new_string) # ๐๏ธ bbyhadz.com # โ Remove the first 3 characters from a string new_string = string[3:] print(new_string) # ๐๏ธ byhadz.com # โ Remove the first 4 characters from a string new_string = string[4:] print(new_string) # ๐๏ธ yhadz.com
We used string slicing to remove the first N characters from a string.
The syntax for string slicing
is my_str[start:stop:step]
.
start
index is inclusive, whereas the stop
index is exclusive (up to, but not including).Python indexes are zero-based, so the first character in a string has an index
of 0
, and the last character has an index of -1
or len(my_str) - 1
.
The slice string[2:]
starts at index 2
and goes to the end of the string.
string = 'bobbyhadz.com' new_string = string[2:] print(new_string) # ๐๏ธ bbyhadz.com
In other words, the slice removes the first 2 characters of the string (indices
0
and 1
).
If you don't need to keep the original variable around, reassign it rather than declaring a new variable.
string = 'bobbyhadz.com' string = string[3:] print(string) # ๐๏ธ byhadz.com
The slice string[3:]
starts at index 3
and goes to the end of the string.
In other words, it removes the first 3 characters from the string (indices 0
,
1
and 2
).
If you only want to remove the first N characters from a string if they are
equal to a certain value, use the str.startswith()
method.
string = 'bobbyhadz.com' substring = 'bob' if string.startswith(substring): string = string[len(substring):] print(string) # ๐๏ธ byhadz.com
The example only removes the first 3 characters from the string if the string starts with the specified characters.
The
str.startswith()
method returns True
if the string starts with the provided prefix, otherwise
the method returns False
.
You can also use string slicing if you need to get the two parts of the string.
string = 'bobbyhadz.com' n = 3 before, after = string[:n], string[n:] print(before) # ๐๏ธ bob print(after) # ๐๏ธ byhadz.com
The slice string[:3]
starts at index 0
and goes up to, but not including
index 3
.
In other words, it returns the first 3 characters in the string (indices 0
,
1
and 2
).
The slice string[3:]
starts at index 3
and goes to the end of the string.
Alternatively, you can use the str.replace()
method.
This is a three-step process:
str.replace()
method to remove the first N characters from the
string.string = 'bobbyhadz.com' n = 2 new_string = string.replace(string[:n], '', 1) print(new_string) # ๐๏ธ bbyhadz.com
We used the str.replace()
method to remove the first N characters from a
string by replacing them with an empty string.
str.replace()
method replaces all occurrences of the provided substring in the string, so we specified a count of 1
to make sure we only replace the first occurrence.The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
The method doesn't change the original string. Strings are immutable in Python.
Alternatively, you can use a for
loop.
This is a three-step process:
for
loop to iterate over the string with enumerate()
.string = 'bobbyhadz.com' new_string = '' n = 3 for index, char in enumerate(string): if index >= n: new_string += char print(new_string) # ๐๏ธ 'byhadz.com'
We used the enumerate()
function to get access to the index of the current
iteration.
string = 'abc' for index, char in enumerate(string): print(char, index) # ๐๏ธ a 0, b 1, c 2
The enumerate() function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.
On each iteration, we check if the current index is greater than or equal to N.
If the condition is met, we add the current character to a new string.
The +=
operator is a shorthand for new_string = new_string + char
.
lstrip
Alternatively, you can use the str.lstrip()
method.
my_str = 'avocado' result_1 = my_str.lstrip(my_str[0:2]) print(result_1) # ๐๏ธ 'ocado' result_2 = my_str.lstrip('av') print(result_2) # ๐๏ธ 'ocado'
The first example uses a slice of 0:2
to get the first 2 characters of the
string.
The str.lstrip() method takes a string containing characters as an argument and returns a copy of the string with the specified leading characters removed.
The method does not change the original string, it returns a new string. Strings are immutable in Python.
str.lstrip()
method doesn't necessarily remove only the first two characters from the string.The method removes all occurrences and combinations of the specified characters from the start of the string.
You can learn more about the related topics by checking out the following tutorials: