user5937546 user5937546 - 1 year ago 69
Python Question

Are there any ways of adding in between strings without changing the string into a list - or are strings immutable?

I am a beginner in Python, and am doing some simple problems. However, I am having trouble with the following question:

Define a simple "spelling correction" function correct() that takes a
string and sees to it that:

1) two or more occurrences of the space character is compressed into
one, and

2) inserts an extra space after a period if the period is directly
followed by a letter. E.g. correct("This (multiple spaces) is (multiple spaces) very funny (multiple spaces) and (multiple spaces) cool.Indeed!") should return "This is very funny and cool. Indeed!"

Tip: Use regular expressions!

This is my code:

def correct(x):
y = x.split()
y = list(y)
for i in y:
if i == " ":
for o in y:
if o == " ":
y.insert(y.index(i)," ")
if i == ".":
y.insert(y.index(i)+1," ")
y = " ".join(y)
return y

print(correct("This is very funny and cool.Indeed!"))

When I ran the program, I got no output. I don't know what I'm doing wrong. Are there any other ways of adding in between strings without changing the string into a list? Or are strings immutable?

Answer Source

Strings are immutable, but act similarly to lists. You don't necessarily need to "convert" them to a list. You can do something like:

myString = "Insert Here -><- Insert There"
myString = myString[:14] + "Inserted" + mystring[14:]
# => myString == "Insert Here ->Inserted<- Insert There"

However, yes, strings are immutable and cannot be 'inserted' into like a list can. For that, you must convert them to a list.

In your case, you may find use in the re regular expression module. In order to compress multiple occurances together, you can do something like:

import re
answer = re.sub(r' +', r' ', myString)

Replace, however, does work because it creates a new instance.

>>> s = "Test"
>>> id(s)
>>> s = s.replace("s", "x")
>>> id(s)
>>> s

So you can easily replace "." with ". " and then run the regular expression to compress it. The end result would look something like:

def correct(myString):
    myString = myString.replace(".", ". ") # Even if there is already a space, it doesn't matter
    import re
    return re.sub(r' +', r' ', myString)

If you don't want, or are not allowed, to use the re module, you can use this one-liner

>>> def correct(myString):
...     return ' '.join(myString.replace('.', '. ').split())
>>> myString = "This   is  very funny  and    cool.Indeed!"
>>> correct(myString)
'This is very funny and cool. Indeed!'

Note that this does not take into account multiple periods or other punctuation types, or if the last character is a period. It will also collapse other forms of whitespace (tabs, etc). So:
"In a galaxy far,far away..."
"In a galaxy far,far away. . ."

We'd recommend using re for this. It's also significantly faster.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download