Replace the first and last word of a string in the pythonic key itself

I am looking for the most pythonic way to replace the first and last word of a string (doing this based on letters will not work for various reasons). To demonstrate what I'm trying to do, here is an example.

a = "this is the demonstration sentence." 

I would like the result of my python function to be:

 b = "This is the demonstration Sentence." 

Its hard part is that there may be spaces at the front or end of the line. I need those that need to be saved.

Here is what I mean:

 a = " this is a demonstration sentence. " 

The result should be:

 b = " This is a demonstration Sentence. " 

He would also be interested in opinions about whether a regular expression would do this job better than python-based built-in methods, or vice versa.

+6
source share
5 answers
 import re a = " this is a demonstration sentence. " print(re.sub(r'''(?x) # VERBOSE mode ( # ^ # start of string \s* # zero-or-more whitespaces \w # followed by an alphanumeric character ) | # OR ( \w # an alphanumeric character \S* # zero-or-more non-space characters \s* # zero-or-more whitespaces $ # end of string ) ''', lambda m: m.group().title(), a)) 

gives

  This is a demonstration Sentence. 
+7
source

This works for you:

 In [9]: a = "this is the demonstration sentence." In [10]: left, _, right = a.strip().partition(' ') In [11]: mid, _, right = right.rpartition(' ') In [12]: Left = left.title() In [13]: Right = right.title() In [14]: a = a.replace(left, Left, 1).replace(right, Right, 1) In [15]: a Out[15]: 'This is the demonstration Sentence.' 
+1
source

There is a regex here:

 def cap(m): return m.group(0).title() re.sub(r'(?:^\s*\w+)|(?:[^\s]+\s*$)',cap," this is a demonstration sentence. ") ' This is a demonstration Sentence. ' 

Sorry, the best I can do ...

Regular Expression Distribution:

 (?:^\s*\w+) #match (optional) whitespace and then 1 word at the beginning of the string | #regex "or" (?:[^\s]+\s*$) #match a string of non-whitespace characters followed by (optional) whitespace and the end of the line. 
+1
source

Similar to inspectorG4dget, but with .rsplit() maxsplit and .capitalize() arguments are specified instead.

Note: .split() also accepts the optional maxsplit argument to split left.

 >>> a = " this is a demonstration sentence. " >>> part_one, part_two = a.rsplit(" ", 1) >>> " ".join([part_one.capitalize(), part_two.capitalize()]) 'This is the demonstration Sentence.' 

.rsplit() splits the text to the right, where the maxsplit argument tells it how many delimiters to execute. A value of 1 will give you one β€œsplit” on the right.

 >>> a.rsplit(" ", 1) ['this is the demonstration', 'sentence.'] 
0
source
 sentence = " this is a demonstration sentence. " sentence = sentence.split(' ') # Split the string where a space occurs for word in sentence: if word: # If the list item is not whitespace sentence[sentence.index(word)] = word.title() break # now that the first word been replaced, we're done # get the last word by traversing the sentence backwards for word in sentence[::-1]: if word: sentence[sentence.index(word)] = word.title() break final_sentence = ' '.join(sentence) 
0
source

All Articles