How do you delete text before a specific character in python?

I'd like to remove all characters before a designated character or set of characters (for example):

intro = "<>I'm Tom."

Now I'd like to remove the <> before I'm (or more specifically, I). Any suggestions?

asked Jun 19, 2015 at 19:20

How do you delete text before a specific character in python?

SaroekinSaroekin

1,0651 gold badge7 silver badges19 bronze badges

4

Use re.sub. Just match all the chars upto I then replace the matched chars with I.

re.sub(r'^.*?I', 'I', stri)

How do you delete text before a specific character in python?

answered Jun 19, 2015 at 19:22

How do you delete text before a specific character in python?

Avinash RajAvinash Raj

169k25 gold badges214 silver badges262 bronze badges

9

str.find could find character index of certain string's first appearance:

intro[intro.find('I'):]

answered Jun 7, 2018 at 12:56

How do you delete text before a specific character in python?

duanduan

8,0193 gold badges47 silver badges67 bronze badges

2

Since index(char) gets you the first index of the character, you can simply do string[index(char):].

For example, in this case index("I") = 2, and intro[2:] = "I'm Tom."

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:

intro = intro[2:]

Instead of knowing where to start, if you know the characters to remove then you could use the lstrip() function:

intro = intro.lstrip("<>")

answered Jun 19, 2015 at 20:15

How do you delete text before a specific character in python?

Brent WashburneBrent Washburne

12.4k4 gold badges58 silver badges78 bronze badges

str = "<>I'm Tom."
temp = str.split("I",1)
temp[0]=temp[0].replace("<>","")
str = "I".join(temp)

answered Jun 19, 2015 at 19:26

How do you delete text before a specific character in python?

3

I looped through the string and passed the index.

intro_list = []

intro = "<>I'm Tom."
for i in range(len(intro)):
    if intro[i] == '<' or intro[i] == '>':
        pass
    else:
        intro_list.append(intro[i])

intro = ''.join(intro_list)
print(intro)

answered Apr 28, 2020 at 7:22

>>> intro = "<>I'm Tom."
#Just split the string at the special symbol

>>> intro.split("<>")

Output = ['', "I'm Tom."]

>>> new = intro.split("<>")

>>> new[1]
"I'm Tom."

answered Feb 1, 2021 at 10:57

import re

date_div = "Blah blah\nblah, Updated: Aug. 23, 2012 Blah blah Updated: Feb. 13, 2019"

up_to_word = ":"
rx_to_first = r'^.*?{}'.format(re.escape(up_to_word))
rx_to_last = r'^.*{}'.format(re.escape(up_to_word))

# (Dot.) In the default mode, this matches any character except a newline. 
# If the DOTALL flag has been specified, this matches any character including a newline.

print("Remove all up to the first occurrence of the word including it:")
print(re.sub(rx_to_first, '', date_div, flags=re.DOTALL).strip())

print("Remove all up to the last occurrence of the word including it:")
print(re.sub(rx_to_last, '', date_div, flags=re.DOTALL).strip())

answered Aug 24, 2020 at 11:13

How do you delete text before a specific character in python?

This solution works if the character is not in the string too, but uses if statements which can be slow.

if 'I' in intro:
  print('I' + intro.split('I')[1])
else:
  print(intro)

answered Jul 11, 2021 at 5:06

How do you delete text before a specific character in python?

You can use itertools.dropwhile to all the characters before seeing a character to stop at. Then, you can use ''.join() to turn the resulting iterable back into a string:

from itertools import dropwhile
''.join(dropwhile(lambda x: x not in stop, intro))

This outputs:

I'm Tom.

answered Apr 10 at 23:26

How do you delete text before a specific character in python?

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:

missing import re

output_str = re.sub(r'^.*?I', 'I', input_str)

answered May 17 at 17:56

quentquent

1,7301 gold badge20 silver badges26 bronze badges

import re
intro = "<>I'm Tom."
re.sub(r'<>I', 'I', intro)

How do you delete text before a specific character in python?

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.