Just use the split function. It returns a list, so you can keep the first element:
>>> s1.split(':')
['Username', ' How are you today?']
>>> s1.split(':')[0]
'Username'
Answer from fredtantini on Stack Overflowpython - how to get the last part of a string before a certain character? - Stack Overflow
Python - How to cut a string in Python? - Stack Overflow
How to remove part of string after certain character in python?
There are a few ways. Here are a few off the top of my head:
>>> s = "abcd//efgh"
>>> s.find("/")
4
>>> s[:s.find("/")]
'abcd'
>>> s.split("/")
['abcd', '', 'efgh']
>>> s.split("/", maxsplit=1)
['abcd', '/efgh']
>>> s.split("/", maxsplit=1)[0]
'abcd'
>>> import re
>>> re.sub("/.*$", "", s)
'abcd'
The last is overkill here and I wouldn't use it, but regexs are often appropriate for doing search & replace operations. Either of the first two would work pretty well. The first depends on the search string appearing though. Otherwise, s.find will return -1 and then s[:-1] will lop off the last character:
>>> s = "abcdef"
>>> s[:s.find("/")]
'abcde' More on reddit.com I am new to python how to remove the first character(letter) from a word in python?
Just use the split function. It returns a list, so you can keep the first element:
>>> s1.split(':')
['Username', ' How are you today?']
>>> s1.split(':')[0]
'Username'
Using index:
>>> string = "Username: How are you today?"
>>> string[:string.index(":")]
'Username'
The index will give you the position of : in string, then you can slice it.
If you want to use regex:
>>> import re
>>> re.match("(.*?):",string).group()
'Username'
match matches from the start of the string.
you can also use itertools.takewhile
>>> import itertools
>>> "".join(itertools.takewhile(lambda x: x!=":", string))
'Username'
You are looking for str.rsplit(), with a limit:
print x.rsplit('-', 1)[0]
.rsplit() searches for the splitting string from the end of input string, and the second argument limits how many times it'll split to just once.
Another option is to use str.rpartition(), which will only ever split just once:
print x.rpartition('-')[0]
For splitting just once, str.rpartition() is the faster method as well; if you need to split more than once you can only use str.rsplit().
Demo:
>>> x = 'http://test.com/lalala-134'
>>> print x.rsplit('-', 1)[0]
http://test.com/lalala
>>> 'something-with-a-lot-of-dashes'.rsplit('-', 1)[0]
'something-with-a-lot-of'
and the same with str.rpartition()
>>> print x.rpartition('-')[0]
http://test.com/lalala
>>> 'something-with-a-lot-of-dashes'.rpartition('-')[0]
'something-with-a-lot-of'
Difference between split and partition is split returns the list without delimiter and will split where ever it gets delimiter in string i.e.
x = 'http://test.com/lalala-134-431'
a,b,c = x.split(-)
print(a)
"http://test.com/lalala"
print(b)
"134"
print(c)
"431"
and partition will divide the string with only first delimiter and will only return 3 values in list
x = 'http://test.com/lalala-134-431'
a,b,c = x.partition('-')
print(a)
"http://test.com/lalala"
print(b)
"-"
print(c)
"134-431"
so as you want last value you can use rpartition it works in same way but it will find delimiter from end of string
x = 'http://test.com/lalala-134-431'
a,b,c = x.rpartition('-')
print(a)
"http://test.com/lalala-134"
print(b)
"-"
print(c)
"431"
Well, to answer the immediate question:
>>> s = "http://www.domain.com/?s=some&two=20"
The rfind method returns the index of right-most substring:
>>> s.rfind("&")
29
You can take all elements up to a given index with the slicing operator:
>>> "foobar"[:4]
'foob'
Putting the two together:
>>> s[:s.rfind("&")]
'http://www.domain.com/?s=some'
If you are dealing with URLs in particular, you might want to use built-in libraries that deal with URLs. If, for example, you wanted to remove two from the above query string:
First, parse the URL as a whole:
>>> import urlparse, urllib
>>> parse_result = urlparse.urlsplit("http://www.domain.com/?s=some&two=20")
>>> parse_result
SplitResult(scheme='http', netloc='www.domain.com', path='/', query='s=some&two=20', fragment='')
Take out just the query string:
>>> query_s = parse_result.query
>>> query_s
's=some&two=20'
Turn it into a dict:
>>> query_d = urlparse.parse_qs(parse_result.query)
>>> query_d
{'s': ['some'], 'two': ['20']}
>>> query_d['s']
['some']
>>> query_d['two']
['20']
Remove the 'two' key from the dict:
>>> del query_d['two']
>>> query_d
{'s': ['some']}
Put it back into a query string:
>>> new_query_s = urllib.urlencode(query_d, True)
>>> new_query_s
's=some'
And now stitch the URL back together:
>>> result = urlparse.urlunsplit((
parse_result.scheme, parse_result.netloc,
parse_result.path, new_query_s, parse_result.fragment))
>>> result
'http://www.domain.com/?s=some'
The benefit of this is that you have more control over the URL. Like, if you always wanted to remove the two argument, even if it was put earlier in the query string ("two=20&s=some"), this would still do the right thing. It might be overkill depending on what you want to do.
You need to split the string:
>>> s = 'http://www.domain.com/?s=some&two=20'
>>> s.split('&')
['http://www.domain.com/?s=some', 'two=20']
That will return a list as you can see so you can do:
>>> s2 = s.split('&')[0]
>>> print s2
http://www.domain.com/?s=some
If I had a string like "1234///5678" and I wanted to remove everything after the first slash, how would I go about it? Thank You in advance.
There are a few ways. Here are a few off the top of my head:
>>> s = "abcd//efgh"
>>> s.find("/")
4
>>> s[:s.find("/")]
'abcd'
>>> s.split("/")
['abcd', '', 'efgh']
>>> s.split("/", maxsplit=1)
['abcd', '/efgh']
>>> s.split("/", maxsplit=1)[0]
'abcd'
>>> import re
>>> re.sub("/.*$", "", s)
'abcd'
The last is overkill here and I wouldn't use it, but regexs are often appropriate for doing search & replace operations. Either of the first two would work pretty well. The first depends on the search string appearing though. Otherwise, s.find will return -1 and then s[:-1] will lop off the last character:
>>> s = "abcdef"
>>> s[:s.find("/")]
'abcde'
+u/CompileBot python
s = "1234///5678"
result = s[:s.find("/") + 1]
print(result)