I've been trying to figure out how to capitalize all the sentences in a string.

In the string

a = "this is the test string! will it work? let's find out. it should work! or should it? oh yes. indeed."

I want all the characters after ". " "! " and "? " to be upper case, like so

a = "This is the test string! Will it work? Let's find out. It should work! Or should it? Oh yes. Indeed."

Method 1:

a = "this is the test string! will it work? let's find out. it should work! or should it? oh yes. indeed."

a = a.split(". ")
for x in a:
	if (x[0].islower()):
		x[0].swapcase() #this line isn't working
a = (". ").join(a)

a = a.split("! ")
for x in a:
	if (x[0].islower()):
		x[0].swapcase() #this line isn't working
a = ("! ").join(a)

a = a.split("? ")
for x in a:
	if (x[0].islower()):
		x[0].swapcase() #this line isn't working
a = ("? ").join(a)

print(a)

There's something wrong with my usage of swapcase(). Probably a rookie mistake, but I can't seem to figure it out.

EDIT: Obviously I'm not storing it correctly, or at all.
However, the most apparent solution to me x[0] = (x[0].swapcase()) results in TypeError: 'str' object does not support item assignment
EDIT#2: Okay, so I realized strings are immutable, rendering method one severely flawed. Going to have to figure out how to create a new string out of the old one with the first letter capitalized.

Method 2:
Another way would be to split the string a in the 3 required places at the same time, using re.compile() and re.split(), thus allowing me to use the much easier str.capitalize(). I've been trying to read up on advanced usage of re, but so far I haven't succeeded in splitting a string in the desired places. Not to mention joining it again.

Method 3:
I'm open to suggestions.


Thanks for reading. Looking forward to basking in your skills :)

Here's a regular expression that should work for you: re.split('([.!?] *)', a) If you need it broken down and explained for you I can do that; otherwise, it may be more fun to investigate using the documentation and pick it apart to learn how it works!

Here's me using your test string:

>>> import re
>>> a = "this is the test string! will it work? let's find out. it should work! or should it? oh yes. indeed."
>>> rtn = re.split('([.!?] *)', a)
>>> ''.join([each.capitalize() for each in rtn])
"This is the test string! Will it work? Let's find out. It should work! Or should it? Oh yes. Indeed."
>>>

At the end there I did a list comprehension. To break it out it would be:

>>> str_pieces = []
>>> for each in rtn:
...     str_pieces.append(each.capitalize())
...     
>>> ''.join(str_pieces)
"This is the test string! Will it work? Let's find out. It should work! Or should it? Oh yes. Indeed."
>>>

Many Thanks! It'll be much clearer to go through the documentation now that I have an example to compare with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.