Replace First and Last Word of String in the Most Pythonic Way

0 votes

Hey guys, I was building a programme for my school computer exercise which involved me to replace the first and last word of a string. Now I've done it using my own indigenous ways but I'm rather interested to learn the most efficient way I could do it using Python

a = "this is the demo sentence."

I'd like the result of my python function to be:

b = "This is the demo Sentence."

The tricky part of it is that there might be spaces on the front or the end of the string. I need those to be preserved.

Here's what I mean:

a = " this is a demonstration sentence. "

The result would need to be:

b = " This is a demonstration Sentence. "

I'm also interested in opinions on whether a regex would do this job better than python's inbuilt methods, or vice versa.

Aug 22, 2018 in Python by charlie_brown
• 7,710 points

edited Aug 23, 2018 by charlie_brown 34 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
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))

yields

 This is a demonstration Sentence. 
answered Aug 22, 2018 by ariaholic
• 7,320 points

Related Questions In Python

0 votes
1 answer

How to print first character of each word in upper case of a string in Python

You can use split() using space as ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
1,210 views
0 votes
1 answer

How to capitalize the first word of string?

Refer to the below command: txt = "hello" x = ...READ MORE

answered Apr 4 in Python by Giri
10 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,710 points
53 views
0 votes
2 answers

Finding the index of a character in python string

You can use word.find('o') as well to ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
68 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
65 views
+4 votes
6 answers
0 votes
1 answer

Can someone explain the behaviour of increment and decrement operators in python

down voteaccepted ++ is not an operator. It is ...READ MORE

answered May 15, 2018 in Python by ariaholic
• 7,320 points
240 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,320 points
50 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.