Solihull
Solihull

Reputation: 7139

How to remove all characters after a specific character in python?

I have a string. How do I remove all text after a certain character? (In this case ...)
The text after will ... change so I that's why I want to remove all characters after a certain one.

Upvotes: 245

Views: 554925

Answers (11)

Rohail
Rohail

Reputation: 11

An easy way using the Python re module

import re, clr
text = 'some string... this part will be removed.'
text= re.search(r'(\A.*)\.\.\..+',url,re.DOTALL|re.IGNORECASE).group(1)
// text = some string
        

Upvotes: 0

Sigmatics
Sigmatics

Reputation: 793

Oneliner for in-place replacement:

text, *_ = text.partition('...')

Credits to original answer: https://stackoverflow.com/a/904758/5726546

Upvotes: 3

Ayman Hourieh
Ayman Hourieh

Reputation: 137156

Assuming your separator is '...', but it can be any string.

text = 'some string... this part will be removed.'
head, sep, tail = text.partition('...')

>>> print head
some string

If the separator is not found, head will contain all of the original string.

The partition function was added in Python 2.5.

S.partition(sep) -> (head, sep, tail)

Searches for the separator sep in S, and returns the part before it, the separator itself, and the part after it. If the separator is not found, returns S and two empty strings.

Upvotes: 141

Alex Martelli
Alex Martelli

Reputation: 881645

Without a regular expression (which I assume is what you want):

def remafterellipsis(text):
  where_ellipsis = text.find('...')
  if where_ellipsis == -1:
    return text
  return text[:where_ellipsis + 3]

or, with a regular expression:

import re

def remwithre(text, there=re.compile(re.escape('...')+'.*')):
  return there.sub('', text)

Upvotes: 11

Ned Batchelder
Ned Batchelder

Reputation: 375584

Split on your separator at most once, and take the first piece:

sep = '...'
stripped = text.split(sep, 1)[0]

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.

Upvotes: 408

Arnaldo C
Arnaldo C

Reputation: 13

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 '/').

path = 'I/only/want/the/containing/directory/not/the/file.txt'

while path[-1] != '/':
    path = path[:-1]

Upvotes: 0

Ganesan J
Ganesan J

Reputation: 646

This is in python 3.7 working to me In my case I need to remove after dot in my string variable fees

fees = 45.05
split_string = fees.split(".", 1)

substring = split_string[0]

print(substring)

Upvotes: 0

Eduardo Freitas
Eduardo Freitas

Reputation: 1057

The method find will return the character position in a string. Then, if you want remove every thing from the character, do this:

mystring = "123⋯567"
mystring[ 0 : mystring.index("⋯")]

>> '123'

If you want to keep the character, add 1 to the character position.

Upvotes: 4

gujaratiraja
gujaratiraja

Reputation: 379

From a file:

import re
sep = '...'

with open("requirements.txt") as file_in:
    lines = []
    for line in file_in:
        res = line.split(sep, 1)[0]
        print(res)

Upvotes: 3

Marcus
Marcus

Reputation: 79

import re
test = "This is a test...we should not be able to see this"
res = re.sub(r'\.\.\..*',"",test)
print(res)

Output: "This is a test"

Upvotes: 6

theannouncer
theannouncer

Reputation: 1212

If you want to remove everything after the last occurrence of separator in a string I find this works well:

<separator>.join(string_to_split.split(<separator>)[:-1])

For example, if string_to_split is a path like root/location/child/too_far.exe and you only want the folder path, you can split by "/".join(string_to_split.split("/")[:-1]) and you'll get root/location/child

Upvotes: 34

Related Questions