>>> 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.
Answer from jamylak on Stack Overflow>>> 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.
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
You were off to a good start. Try this in your loop:
for line in lines:
line = line[2:]
# do something here
The [2:] is called "slice" syntax, it essentially says "give me the part of this sequence which begins at index 2 and continues to the end (since no end point was specified after the colon).
String slicing will help you:
>>> a="Some very long string"
>>> a[2:]
'me very long string'
string=input("Enter a string: ")How to remove the first letter of the string?
thank you
python 2.x
s = ":dfa:sif:e"
print s[1:]
python 3.x
s = ":dfa:sif:e"
print(s[1:])
both prints
dfa:sif:e
Your problem seems unclear. You say you want to remove "a character from a certain position" then go on to say you want to remove a particular character.
If you only need to remove the first character you would do:
s = ":dfa:sif:e"
fixed = s[1:]
If you want to remove a character at a particular position, you would do:
s = ":dfa:sif:e"
fixed = s[0:pos]+s[pos+1:]
If you need to remove a particular character, say ':', the first time it is encountered in a string then you would do:
s = ":dfa:sif:e"
fixed = ''.join(s.split(':', 1))