Reputation: 13
Pretty new to coding so what I have here is probably a mess.
What I'm trying to do is remove all of a few characters from a given string (".", "?", "!") except for the last character.
So I aimed to get the sentence
The. big? brown! fox. jumped? over! the. lazy? dog.
to
The big brown fox jumped over the lazy dog.
by using the .replace function to replace, for example, "." with "", but using [0:-2] to make the command ignore the last character (which is a ".") so that it won't get removed.
Here's the code
x = ("The. big? brown! fox. jumped? over! the. lazy? dog.")
x = (x[0:-2].replace(".", "")) + (x[-1])
x = (x[0:-2].replace("?", "")) + (x[-1])
x = (x[0:-2].replace("!", "")) + (x[-1])
print(x)
I expected the result to be:
The big brown fox jumped over the lazy dog.
However, the result I got was:
The big brown fox jumped over the lazy .
with the last word, dog, removed.
I've tried messing about and changing the position of the code around a bit, but all it seems to do is make the problem worse, and I couldn't find anything online. I assume it's to do with me using:
x[0:-2]
but I'm not sure. So what's causing the printed string to suddenly lost a bunch of characters? And how can I fix the code to change it, or am I going to have to start again from scratch?
Edit: This has been solved, thanks for the help guys!
Upvotes: 1
Views: 91
Reputation: 6598
This could be solved using a regex.
>>> import re
>>> s = 'The. big? brown! fox. jumped? over! the. lazy? dog.'
>>> pattern = re.compile(r'[?!.](?!$)')
>>> s = re.sub(pattern, '', s)
>>> s
'The big brown fox jumped over the lazy dog.'
Upvotes: 0
Reputation: 470
Each of the three lines of code that are using replace()
removes one character from your string.
For example, running just the first command
x = (x[0:-2].replace(".", "")) + (x[-1])
gives the output as The big? brown! fox jumped? over! the lazy? do.
This is because x[0:2]
refers to The. big? brown! fox. jumped? over! the. lazy? do
(i.e. it removes the last 2 characters of the string).
Then, the .
is removed due to the replace function to give The big? brown! fox jumped? over! the lazy? do
and then x[-1]
(which is .
) is appended to the string to give The big? brown! fox jumped? over! the lazy? do.
.
This happens thrice, therefore removing the whole word "dog".
Change x[0:-2]
to x[0:-1]
and it'll work.
Upvotes: 0
Reputation: 38415
You can use regex,
s = re.sub('[^a-zA-Z0-9\\s]', '',s[:-1]) + s[-1]
Upvotes: 0
Reputation: 16772
Why not use search and replace:
x = "The. big? brown! fox. jumped? over! the. lazy? dog."
for chr in ".?!":
x = x.replace(chr,"")
print(x + '.')
OUTPUT:
The big brown fox jumped over the lazy dog.
Upvotes: 0
Reputation: 196
You could do a chain of replace
to simplify your code. Here is a solution for you:
x = x.replace("?", "").replace("!", "").replace(".","",3)
Notice when you replace "."
, you will need the count to replace the three first occurrence of "."
Upvotes: 0
Reputation: 469
Please look at the "Updated" part of the code.
Code:
print("Original: ")
x = ("The. big? brown! fox. jumped? over! the. lazy? dog.")
x = (x[0:-2].replace(".", "")) + (x[-1])
print(x)
x = (x[0:-2].replace("?", "")) + (x[-1])
print(x)
x = (x[0:-2].replace("!", "")) + (x[-1])
print(x)
print("-------------------------------------------------------")
print("Updated: ")
x = ("The. big? brown! fox. jumped? over! the. lazy? dog.")
x = (x[0:].replace(".", ""))
print(x)
x = (x[0:].replace("?", ""))
print(x)
x = (x[0:].replace("!", ""))
print(x)
Output:
Original:
The big? brown! fox jumped? over! the lazy? do.
The big brown! fox jumped over! the lazy d.
The big brown fox jumped over the lazy .
-------------------------------------------------------
Updated:
The big? brown! fox jumped? over! the lazy? dog
The big brown! fox jumped over! the lazy dog
The big brown fox jumped over the lazy dog
Upvotes: 0
Reputation: 657
First of all, you're using a lot of parenthesis where you shouldn't be.
x = "The. big? brown! fox. jumped? over! the. lazy? dog."
x = x[0:-2].replace(".", "") + x[-1]
x = x[0:-2].replace("?", "") + x[-1]
x = x[0:-2].replace("!", "") + x[-1]
print(x)
this is equivalent to the code in your question.
Next, you can either loop through the same replace call or link your replace statements, they don't have to be on different lines.
x = "The. big? brown! fox. jumped? over! the. lazy? dog."
for character in ['.', '?', '!']:
x = x[0:-2].replace(character, '') + x[-1]
print(x)
is the same as:
x = "The. big? brown! fox. jumped? over! the. lazy? dog."
x = x[0:-2].replace('.', '').replace('?', '').replace('!', '') + x[-1]
print(x)
Finally, the place where you're losing letters is in the mismatch between x[0:-2]
and x[-1]
:
>>> x = "The. big? brown! fox. jumped? over! the. lazy? dog."
>>> x = x[0:-2] + x[-1]
>>> x
'The. big? brown! fox. jumped? over! the. lazy? do.'
>>> x = x[0:-2] + x[-1]
>>> x
'The. big? brown! fox. jumped? over! the. lazy? d.'
>>> x = x[0:-2] + x[-1]
>>> x
'The. big? brown! fox. jumped? over! the. lazy? .'
If you only wanted to exclude the last character, you want to use x[0:-1]
, leaving you with:
x = "The. big? brown! fox. jumped? over! the. lazy? dog."
for character in ['.', '?', '!']:
x = x[0:-1].replace(character, '') + x[-1]
print(x)
>>> The big brown fox jumped over the lazy dog.
Upvotes: 1
Reputation: 649
You have to use x[0:-1]
because the Python slice operator stops at stop-1
. Thus x[0:-2]
remove the last character every time which explains why the last 3 letters are cut off.
Upvotes: 0