I'd like to remove all characters before a designated character or set of characters (for example): Show
Now I'd like to remove the asked Jun 19, 2015 at 19:20
SaroekinSaroekin 1,0651 gold badge7 silver badges19 bronze badges 4 Use
answered Jun 19, 2015 at 19:22
Avinash RajAvinash Raj 169k25 gold badges214 silver badges262 bronze badges 9
answered Jun 7, 2018 at 12:56
duanduan 8,0193 gold badges47 silver badges67 bronze badges 2 Since For
example, in this case answered Jun 19, 2015 at 19:22
AshkayAshkay 7861 gold badge7 silver badges17 bronze badges 3 If you know the character position of where to start deleting, you can use slice notation:
Instead of knowing where to start, if you know the characters to remove then you could use the lstrip() function:
answered Jun 19, 2015 at 20:15
Brent WashburneBrent Washburne 12.4k4 gold badges58 silver badges78 bronze badges
answered Jun 19, 2015 at 19:26
3 I looped through the string and passed the index.
answered Apr 28, 2020 at 7:22
answered Feb 1, 2021 at 10:57
answered Aug 24, 2020 at 11:13
This solution works if the character is not in the string too, but uses if statements which can be slow.
answered Jul 11, 2021 at 5:06
You can use
This outputs:
answered Apr 10 at 23:26
BrokenBenchmarkBrokenBenchmark 16k7 gold badges18 silver badges29 bronze badges Based on the @AvinashRaj answer, you can use re.sub to substituate a substring by a string or a character thanks to regex:
answered May 17 at 17:56
quentquent 1,7301 gold badge20 silver badges26 bronze badges
Tunaki 128k45 gold badges321 silver badges405 bronze badges answered Jun 5, 2016 at 5:58
1 How do you delete a text before and after a specific character in Python?To remove everything after a character in a string:. Use the str. split() method to split the string on the separator.. Access the list element at index 0 to get everything before the separator.. Optionally, use the addition (+) operator to add the separator.. How do I delete all text before a character in Python?If you need to remove everything before last occurrence of a character, use the str. rfind() method. Copied! The str.. Use the str. ... . Use string slicing and set the start index to the index of the character.. How do you delete something from a specific character in Python?Use str.. a_string = "ab-cd". split_string = a_string. split("-", 1) Split into "ab" and "cd". substring = split_string[0]. print(substring). How do you delete something before a space in Python?strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.
|