Select last chars of string until whitespace in Python [duplicate] - python

This question already has answers here:
Python: Cut off the last word of a sentence?
(10 answers)
Closed 8 years ago.
Is there any efficient way to select the last characters of a string until there's a whitespace in Python?
For example I have the following string:
str = 'Hello my name is John'
I want to return 'John'. But if the str was:
str = 'Hello my name is Sally'
I want to retrun 'Sally'

Just split the string on whitespace, and get the last element of the array. Or use rsplit() to start splitting from end:
>>> st = 'Hello my name is John'
>>> st.rsplit(' ', 1)
['Hello my name is', 'John']
>>>
>>> st.rsplit(' ', 1)[1]
'John'
The 2nd argument specifies the number of split to do. Since you just want last element, we just need to split once.
As specified in comments, you can just pass None as 1st argument, in which case the default delimiter which is whitespace will be used:
>>> st.rsplit(None, 1)[-1]
'John'
Using -1 as index is safe, in case there is no whitespace in your string.

It really depends what you mean by efficient, but the simplest (efficient use of programmer time) way I can think of is:
str.split()[-1]
This fails for empty strings, so you'll want to check that.

I think this is what you want:
str[str.rfind(' ')+1:]
this creates a substring from str starting at the character after the right-most-found-space, and up until the last character.
This works for all strings - empty or otherwise (unless it's not a string object, e.g. a None object would throw an error)

Related

.strip('{}:'.format(key)) sometimes strips last characters [duplicate]

This question already has answers here:
Python strip unexpected behavior
(2 answers)
Closed 4 years ago.
value = div.xpath('normalize-space(.)').extract()[0].strip('{}:'.format(key)).strip()
The code above sometimes strips the last character from the word. After removing the code after extract() all the data came back fine but in a list.
Example :
Unknown from Duration: Unknown turns into unknow
Movie from Type: Movie turns into Movi
Why does this happen?
I tried this in Python shell and it also strips the last characters
>>> value = ['Type: Movie']
>>> value[0].strip('{}:'.format('Type')).strip()
'Movi'
I expect it to return Movie instead of e getting stripped.
It seems that this .strip('{}:'.format('Type')) is responsible. I removed the last strip() it only return data with spaces.
Edit: It seems that strip() takes characters in inputted string and remove them instead of removing exact strings. That is why the data came out broken. I think a string split then slice is good.
Edit 2:
Seems like answers by Austin and Pankaj Singhal is good and bug free for my use case.
Use a split on 'Type: ' and take the second item:
value = ['Type: Movie']
print(value[0].split('Type: ')[1])
# Movie
Talking about your code, strip is not meant for what you are trying to do. strip only removes characters from at the ends.
You could use lstrip (which returns a copy of the string with only leading characters removed), instead of strip (which returns a copy of the string with leading and trailing characters removed):
>>> 'Type: Movie'.lstrip("Type:").strip()
'Movie'
>>> 'Type: Something with Type'.lstrip("Type:").strip()
'Something with Type'
>>> 'Type: Something with Type:'.lstrip("Type:").strip()
'Something with Type:'
>>>
OR:
>>> value = ['Type: Movie']
>>> value[0][value[0].find(':')+2:]
'Movie'
>>>
And of course, this is another option similar to the first one, just using lstrip:
>>> value[0][value[0].find(':')+1:].lstrip()
'Movie'
>>>
OR:
>>> value[0].lstrip(value[0][:value[0].find(':')+2])
'Movie'
Note: here find can be replaced with index
str.strip does not strip that exact string, but each character in that string, i.e. strip("Type:") will remove each T, y, p, etc. from the beginning and end of the string.
Instead, you could use a regular expression with the ^ anchor to only match substrings at the beginning of the string.
>>> value = ['Type: Movie with Type: in its name']
>>> key = "Type"
>>> re.sub(r"^{}: ".format(key), "", value[0])
'Movie with Type: in its name'

Python - Remove a character the second time it is duplicated [duplicate]

This question already has answers here:
How to find and replace nth occurrence of word in a sentence using python regular expression?
(9 answers)
Closed 6 years ago.
I'm looking to remove a ',' (comma) from a string, but only the second time the comma occurs as it needs to be in the correct format for reverse geocoding...
As an example I have the following string in python:
43,14,3085
How would I convert it to the following format:
43,143085
I have tried using regex and str.split() but have not achieved result yet..
If you're sure that string only contains two commas and you want to remove the last one you can use rsplit with join:
>>> s = '43,14,3085'
>>> ''.join(s.rsplit(',', 1))
'43,143085'
In above rsplit splits starting from the end number of times given as a second parameter:
>>> parts = s.rsplit(',', 1)
>>> parts
['43,14', '3085']
Then join is used to combine the parts together:
>>> ''.join(parts)
'43,143085'
What about something like:
i = s.find(',')
s[:i] + ',' + s[i+1:].replace(",", "")
This will get rid of all your commas excepts the first one:
string = '43,14,3085'
splited = string.split(',')
string=",".join(splited[0:2])
string+="".join(splited[2:])
print(string)

How to split strings by only the first special character that's within the string? [duplicate]

This question already has answers here:
Splitting on first occurrence
(5 answers)
Closed 8 years ago.
I'd like to split the below string by only the first equal sign in that string
String:
s= 'ButtonParams=U3ViamVjdCxFbWFpbA=='
Desired_String:
s= ['ButtonParams','U3ViamVjdCxFbWFpbA==']
When I do s.split("="), I get the following, which is what I do not want:
s.split("=")
['ButtonParams', 'U3ViamVjdCxFbWFpbA', '', '']
I need to run this function across a list of strings where this is the case, so scalability is important here.
split accepts an optional "maxsplit" parameter: if you set it to 1 it will split on the first = character it finds and return the remainder of the string:
>>> s.split('=', 1)
['ButtonParams', 'U3ViamVjdCxFbWFpbA==']
s.split("=", maxsplit=1) is the best but
import re
print (re.split('=',s,1))
The output is
['ButtonParams', 'U3ViamVjdCxFbWFpbA==']
As you have tagged regex
A little deviation from the post
If the expected output was ['ButtonParams', 'U3ViamVjdCxFbWFpbA'] then you can have the following liscos (list comprehensions)
[i for i in s.split('=') if i is not '']
[i for i in s.split('=') if i ] (Contributed by Adam Smith)
str.split accepts an optional argument maxsplit which determines how many splits (maximally) to make, e.g.:
"ButtonParams=U3ViamVjdCxFbWFpbA==".split("=", maxsplit=1)
# ['ButtonParams', 'U3ViamVjdCxFbWFpbA==']

Why does str.lstrip strip an extra character? [duplicate]

This question already has answers here:
How do I remove a substring from the end of a string?
(24 answers)
Closed 4 years ago.
>>> path = "/Volumes/Users"
>>> path.lstrip('/Volume')
's/Users'
>>> path.lstrip('/Volumes')
'Users'
>>>
I expected the output of path.lstrip('/Volumes') to be '/Users'
lstrip is character-based, it removes all characters from the left end that are in that string.
To verify this, try this:
"/Volumes/Users".lstrip("semuloV/") # also returns "Users"
Since / is part of the string, it is removed.
You need to use slicing instead:
if s.startswith("/Volumes"):
s = s[8:]
Or, on Python 3.9+ you can use removeprefix:
s = s.removeprefix("/Volumes")
Strip is character-based. If you are trying to do path manipulation you should have a look at os.path
>>> os.path.split("/Volumes/Users")
('/Volumes', 'Users')
The argument passed to lstrip is taken as a set of characters!
>>> ' spacious '.lstrip()
'spacious '
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'
See also the documentation
You might want to use str.replace()
str.replace(old, new[, count])
# e.g.
'/Volumes/Home'.replace('/Volumes', '' ,1)
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
For paths, you may want to use os.path.split(). It returns a list of the paths elements.
>>> os.path.split('/home/user')
('/home', '/user')
To your problem:
>>> path = "/vol/volume"
>>> path.lstrip('/vol')
'ume'
The example above shows, how lstrip() works. It removes '/vol' starting form left. Then, is starts again...
So, in your example, it fully removed '/Volumes' and started removing '/'. It only removed the '/' as there was no 'V' following this slash.
HTH
lstrip doc says:
Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping
So you are removing every character that is contained in the given string, including both 's' and '/' characters.
Here is a primitive version of lstrip (that I wrote) that might help clear things up for you:
def lstrip(s, chars):
for i in range len(s):
char = s[i]
if not char in chars:
return s[i:]
else:
return lstrip(s[i:], chars)
Thus, you can see that every occurrence of a character in chars is is removed until a character that is not in chars is encountered. Once that happens, the deletion stops and the rest of the string is simply returned

Ways to slice a string?

I have a string, example:
s = "this is a string, a"
Where a ',' (comma) will always be the 3rd to the last character, aka s[-3].
I am thinking of ways to remove the ',' but can only think of converting the string into a list, deleting it, and converting it back to a string. This however seems a bit too much for simple task.
How can I accomplish this in a simpler way?
Normally, you would just do:
s = s[:-3] + s[-2:]
The s[:-3] gives you a string up to, but not including, the comma you want removed ("this is a string") and the s[-2:] gives you another string starting one character beyond that comma (" a").
Then, joining the two strings together gives you what you were after ("this is a string a").
A couple of variants, using the "delete the last comma" rather than "delete third last character" are:
s[::-1].replace(",","",1)[::-1]
or
''.join(s.rsplit(",", 1))
But these are pretty ugly. Slightly better is:
a, _, b = s.rpartition(",")
s = a + b
This may be the best approach if you don't know the comma's position (except for last comma in string) and effectively need a "replace from right". However Anurag's answer is more pythonic for the "delete third last character".
Python strings are immutable. This means that you must create at least 1 new string in order to remove the comma, as opposed to editing the string in place in a language like C.
For deleting every ',' character in the text, you can try
s = s.split(',')
>> ["this is a string", " a"]
s = "".join(s)
>> "this is a string a"
Or in one line:
s0 = "".join(s.split(','))
The best simple way is : You can use replace function as :-
>>> s = 'this is a string, a'
>>> s = s.replace(',','')
>>> s
'this is a string a'
Here, replace() function search the character ',' and replace it by '' i.e. empty character
Note that , the replace() function defaults all ',' but if you want only replace some ',' in some case you can use : s.replace(',' , '', 1)
To slice a string of arbitrary length into multiple equal length slices of arbitrary length you could do
def slicer(string, slice_length):
return [string[i:i + slice_length]
for i in xrange(0, len(string), slice_length)]
If slice_length does not divide exactly into len(string) then there will be a single slice at the end of the list that holds the remainder.

Categories

Resources