Monday 29 July 2019

python - Remove the first character of a string

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))

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...