2

does anybody know how to delete all characters behind a specific character??

like this:

http://google.com/translate_t

into

http://google.com
1
  • This isn't really deleting -- Python can't update a string. This is extracting a substring up to a given position. You might want to rephrase your question. Commented Feb 14, 2009 at 16:21

4 Answers 4

6

if you're asking about an abstract string and not url you could go with:

>>> astring ="http://google.com/translate_t"
>>> astring.rpartition('/')[0]
http://google.com
Sign up to request clarification or add additional context in comments.

4 Comments

string is a builtin module in Python therefore it is bad to use it as a variable name.
@jfs: it's a standard library module. it may or may not clash with certain variable names.
Note the rpartition method only exists in Python 2.5+, for earlier versions you'd need to use rsplit('/', 1).
@Carl Meyer actually rsplit("/", 1)[0]
5

For urls, using urlparse:

>>> import urlparse
>>> parts = urlparse.urlsplit('http://google.com/path/to/resource?query=spam#anchor')
>>> parts
('http', 'google.com', '/path/to/resource', 'query=spam', 'anchor')
>>> urlparse.urlunsplit((parts[0], parts[1], '', '', ''))
'http://google.com'

For arbitrary strings, using re:

>>> import re
>>> re.split(r'\b/\b', 'http://google.com/path/to/resource', 1)
['http://google.com', 'path/to/resource']

Comments

2
str="http://google.com/translate_t"
shortened=str[0:str.rfind("/")]

Should do it. str[a:b] returns a substring in python. And rfind is used to find the index of a character sequence, starting at the end of the string.

1 Comment

Also, if there is no "/" in the string, this returns the initial string shortened by one character.
2

If you know the position of the character then you can use the slice syntax to to create a new string:

In [2]: s1 = "abc123"
In [3]: s2 = s1[:3]
In [4]: print s2
abc

To find the position you can use the find() or index() methods of strings. The split() and partition() methods may be useful, too. Those methods are documented in the Python docs for sequences.

To remove a part of a string is imposible because strings are immutable.

If you want to process URLs then you should definitely use the urlparse library. It lets you split an URL into its parts. If you just want remove a part of the file path then you will have to do that still by yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.