I have a string. How do I remove all text after a certain character? (In this case Show
wjandrea 24.5k8 gold badges53 silver badges73 bronze badges asked May 24, 2009 at 21:56
0 Split on your separator at most once, and take the first piece:
You didn't say what should happen if the separator isn't present. Both this and Alex's solution will return the entire string in that case.
wjandrea 24.5k8 gold badges53 silver badges73 bronze badges answered May 24, 2009 at 22:01
Ned BatchelderNed Batchelder 351k71 gold badges551 silver badges649 bronze badges 4 Assuming your separator is '...', but it can be any string.
If the separator is not found, The partition function was added in Python 2.5.
wjandrea 24.5k8 gold badges53 silver badges73 bronze badges answered May 24, 2009 at 22:02
Ayman HouriehAyman Hourieh 125k22 gold badges140 silver badges115 bronze badges 5 If you want to remove everything after the last occurrence of separator in a string I find this works well:
For example, if answered Sep 14, 2015 at 22:18
theannouncertheannouncer 1,08115 silver badges26 bronze badges 1 Without a regular expression (which I assume is what you want):
or, with a regular expression:
wjandrea 24.5k8 gold badges53 silver badges73 bronze badges answered May 24, 2009 at 22:00
Alex MartelliAlex Martelli 823k163 gold badges1202 silver badges1379 bronze badges 3
Output: "This is a test" answered Feb 26, 2020 at 14:12
MarcusMarcus 691 silver badge4 bronze badges 1 From a file:
answered Mar 19, 2020 at 23:42
The method find will return the character position in a string. Then, if you want remove every thing from the character, do this:
If you want to keep the character, add 1 to the character position. answered Jun 18, 2020 at 2:42
fees = 45.05 substring = split_string[0] print(substring) answered Mar 3, 2021 at 10:54
Ganesan JGanesan J 3835 silver badges9 bronze badges Yet another way to remove all characters after the last occurrence of a character in a string (assume that you want to remove all characters after the final '/').
answered May 25, 2021 at 22:18
1 another easy way using re will be
answered May 20, 2015 at 10:42
1 How do you remove a string after a 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 remove a string after a character?Use the String. slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str. indexOf('[')); .
How do you delete text before a specific character in Python?To remove everything before the first occurrence of the character '-' in a string, pass the character '-' as separator and 1 as the max split value. The split('-', 1) function will split the string into 2 parts, Part 1 should contain all characters before the first occurrence of character '-'.
How do you remove a string from a string in Python?In Python you can use the replace() and translate() methods to specify which characters you want to remove from a string and return a new modified string result. It is important to remember that the original string will not be altered because strings are immutable.
|