How might one remove the first x characters from a string? For example, if one had a string lipsum
, how would they remove the first 3 characters and get a result of sum
?
asked Aug 4, 2012 at 6:45
1
>>> text = 'lipsum'
>>> text[3:]
'sum'
See the official documentation on strings for more information and this SO answer for a concise summary of the notation.
answered Aug 4, 2012 at 6:45
jamylakjamylak
124k29 gold badges227 silver badges227 bronze badges
0
Another way [depending on your actual needs]: If you want to pop the first n characters and save both the popped characters and the modified string:
s = 'lipsum'
n = 3
a, s = s[:n], s[n:]
print[a]
# lip
print[s]
# sum
phoenix
6,4484 gold badges36 silver badges44 bronze badges
answered Dec 18, 2013 at 17:10
Ken AKen A
3612 silver badges4 bronze badges
1
>>> x = 'lipsum'
>>> x.replace[x[:3], '']
'sum'
answered Aug 4, 2012 at 6:45
tkbxtkbx
14.8k28 gold badges82 silver badges120 bronze badges
2
Use del
.
Example:
>>> text = 'lipsum'
>>> l = list[text]
>>> del l[3:]
>>> ''.join[l]
'sum'
answered Oct 19, 2018 at 5:58
U12-ForwardU12-Forward
66.2k13 gold badges77 silver badges96 bronze badges
4
Example to show last 3 digits of account number.
x = '1234567890'
x.replace[x[:7], '']
o/p: '890'
answered Dec 13, 2016 at 10:21
3
In this article we will discuss ways to remove first N characters from a string either by using slicing or regex or simple for loop. In python, we can use slicing to select a specific range of characters in a string i.e. It returns the characters of the string
from index position start to end -1, as a new string. Default values of start & end are 0 & Z respectively, where Z is the size of the string. It means if neither start nor end are provided, then it selects all characters in the string i.e. from 0 to size-1 and creates a new string from those characters. We can use this slicing technique to slice out a piece of string, that includes all characters of string except the first N characters. Let’s see how to do that, Advertisements
Suppose our string contains N characters. We will slice the string to select characters from index position 3 to N and then assign it back to the variable i.e. Output By selecting characters in string from index position 3 to N, we selected all characters from string except the first 3 characters and then assigned this sliced string object back to the same variable again, to give an effect that first 3
characters of string are deleted. To remove the first character from string, slice the string to select characters from index 1 till the end of string i.e. Output: It selected all characters in the string except the first one. To delete first N character from a string, we can iterate over the characters of string one by one and
select all characters from index position N till the end of the string. We have created a function for this, It accepts a string and a number N as arguments. Returns a new string that contains all the characters of the given string, except first N characters. Let’s use this function in some examples, Output Output We can use regex in python, to match 2 groups in a string i.e. Then modify the string by substituting both the groups by a single group i.e. group 2. Output Here sub[] function matched the given pattern in the string and passed the
matched object to our call-back function remove_first_group[]. Match object internally contains two groups and the function remove_first_group[] returned a string by selecting characters from group 2 only. Then sub[] function replaced the matched characters in the original string by the characters returned by the remove_first_group[] function. So, this is how we can delete first N characters from a string in python To remove first character from string using regex, use n as 1
i.e. Output The complete example is as follows, Output:Use slicing to remove first N character from string in python
str[start:end]
Remove first 3 character from string using Slicing
org_string = "Sample String"
# Slice string to remove first 3 characters from string
mod_string = org_string[3:]
print[mod_string]
ple String
Remove first character from string using slicing
org_string = "Sample String"
# Slice string to remove first 3 characters from string
mod_string = org_string[1:]
print[mod_string]
ample String
Use for-loop to remove first N character from string in python
def remove_first_n_char[org_str, n]:
""" Return a string by deleting first n
characters from the string """
mod_string = ""
for i in range[n, len[org_str]]:
mod_string = mod_string + org_str[i]
return mod_string
Remove first character from string using for loop
org_string = "Sample String"
# Remove first character from string
mod_string = remove_first_n_char[org_string, 1]
print[mod_string]
ample String
Remove first 3 characters from string using for loop
org_string = "Sample String"
# Remove first character from string
mod_string = remove_first_n_char[org_string, 3]
print[mod_string]
ple String
Remove
First N character from string using regex
Remove first 3 characters from string using regex
import re
def remove_first_group[m]:
""" Return only group 1 from the match object
Delete other groups """
return m.group[2]
org_string = "Sample String"
result = re.sub["[^.{3}][.*]", remove_first_group, org_string]
print[result]
ple String
import re
def remove_first_group[m]:
""" Return only group 1 from the match object Delete other groups """
return m.group[2]
org_string = "Sample String"
result = re.sub["[^.{1}][.*]", remove_first_group, org_string]
print[result]
ample String
import re
def remove_first_group[m]:
""" Return only group 1 from the match object
Delete other groups """
return m.group[2]
def remove_first_n_char[org_str, n]:
""" Return a string by deleting first n
characters from the string """
mod_string = ""
for i in range[n, len[org_str]]:
mod_string = mod_string + org_str[i]
return mod_string
def main[]:
print['*** Remove first N character from string using Slicing ***']
print['*** Remove first 3 characters from string using slicing ***']
org_string = "Sample String"
# Slice string to remove first 3 characters from string
mod_string = org_string[3:]
print[mod_string]
print['*** Remove first character from string using slicing ***']
org_string = "Sample String"
# Slice string to remove first 3 characters from string
mod_string = org_string[1:]
print[mod_string]
print['***** Remove first N character from string using for loop *****']
print['*** Remove first character from string using for loop ***']
org_string = "Sample String"
# Remove first character from string
mod_string = remove_first_n_char[org_string, 1]
print[mod_string]
print['*** Remove first 3 characters from string using for loop ***']
org_string = "Sample String"
# Remove first character from string
mod_string = remove_first_n_char[org_string, 3]
print[mod_string]
print['***** Remove first N characters from string using regex *****']
print['*** Remove first character from string using regex ***']
org_string = "Sample String"
result = re.sub["[^.{1}][.*]", remove_first_group, org_string]
print[result]
print['*** Remove first 3 characters from string using regex***']
org_string = "Sample String"
result = re.sub["[^.{3}][.*]", remove_first_group, org_string]
print[result]
if __name__ == '__main__':
main[]
*** Remove first N character from string using Slicing ***
*** Remove first 3 characters from string using slicing ***
ple String
*** Remove first character from string using slicing ***
ample String
***** Remove first N character from string using for loop *****
*** Remove first character from string using for loop ***
ample String
*** Remove first 3 characters from string using for loop ***
ple String
***** Remove first N characters from string using regex *****
*** Remove first character from string using regex ***
ample String
*** Remove first 3 characters from string using regex***
ple String