Removing extra whitespace between words using for loop Python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I need to remove all excess white space and leave one space, between my words while only using if and while statements. and then state the amount of characters that have been removed and the new sentence
edit, it must also work for punctuation included within the sentence.
This is what I have come up with however it leaves me with only the first letter of the sentence i choose as both the number, and the final sentence. can anyone Help.
def cleanupstring(S):
lasti = ""
result = ""
for i in S:
if lasti == " " and i == " ":
i = ""
else:
lasti = i
result += i
return result
sentence = input("Enter a string: ")
outputList = cleanupstring(sentence)
print("A total of", outputList[1], "characters have been removed from your string.")
print("The new string is:", outputList[0])
python
add a comment |
I need to remove all excess white space and leave one space, between my words while only using if and while statements. and then state the amount of characters that have been removed and the new sentence
edit, it must also work for punctuation included within the sentence.
This is what I have come up with however it leaves me with only the first letter of the sentence i choose as both the number, and the final sentence. can anyone Help.
def cleanupstring(S):
lasti = ""
result = ""
for i in S:
if lasti == " " and i == " ":
i = ""
else:
lasti = i
result += i
return result
sentence = input("Enter a string: ")
outputList = cleanupstring(sentence)
print("A total of", outputList[1], "characters have been removed from your string.")
print("The new string is:", outputList[0])
python
3
Possible duplicate of Cleaning up a string without split/strip/built-in functions
– Adam
Nov 23 '18 at 18:55
1
One easy:print("The new string is:", outputList)
.
– Austin
Nov 23 '18 at 18:56
You only print the first item if the string
– Tobias Wilfert
Nov 23 '18 at 18:57
Also outputList[1] is not the amount of characters that have been removed
– Tobias Wilfert
Nov 23 '18 at 18:58
add a comment |
I need to remove all excess white space and leave one space, between my words while only using if and while statements. and then state the amount of characters that have been removed and the new sentence
edit, it must also work for punctuation included within the sentence.
This is what I have come up with however it leaves me with only the first letter of the sentence i choose as both the number, and the final sentence. can anyone Help.
def cleanupstring(S):
lasti = ""
result = ""
for i in S:
if lasti == " " and i == " ":
i = ""
else:
lasti = i
result += i
return result
sentence = input("Enter a string: ")
outputList = cleanupstring(sentence)
print("A total of", outputList[1], "characters have been removed from your string.")
print("The new string is:", outputList[0])
python
I need to remove all excess white space and leave one space, between my words while only using if and while statements. and then state the amount of characters that have been removed and the new sentence
edit, it must also work for punctuation included within the sentence.
This is what I have come up with however it leaves me with only the first letter of the sentence i choose as both the number, and the final sentence. can anyone Help.
def cleanupstring(S):
lasti = ""
result = ""
for i in S:
if lasti == " " and i == " ":
i = ""
else:
lasti = i
result += i
return result
sentence = input("Enter a string: ")
outputList = cleanupstring(sentence)
print("A total of", outputList[1], "characters have been removed from your string.")
print("The new string is:", outputList[0])
python
python
edited Nov 23 '18 at 19:17
Funnacho
asked Nov 23 '18 at 18:52
FunnachoFunnacho
32
32
3
Possible duplicate of Cleaning up a string without split/strip/built-in functions
– Adam
Nov 23 '18 at 18:55
1
One easy:print("The new string is:", outputList)
.
– Austin
Nov 23 '18 at 18:56
You only print the first item if the string
– Tobias Wilfert
Nov 23 '18 at 18:57
Also outputList[1] is not the amount of characters that have been removed
– Tobias Wilfert
Nov 23 '18 at 18:58
add a comment |
3
Possible duplicate of Cleaning up a string without split/strip/built-in functions
– Adam
Nov 23 '18 at 18:55
1
One easy:print("The new string is:", outputList)
.
– Austin
Nov 23 '18 at 18:56
You only print the first item if the string
– Tobias Wilfert
Nov 23 '18 at 18:57
Also outputList[1] is not the amount of characters that have been removed
– Tobias Wilfert
Nov 23 '18 at 18:58
3
3
Possible duplicate of Cleaning up a string without split/strip/built-in functions
– Adam
Nov 23 '18 at 18:55
Possible duplicate of Cleaning up a string without split/strip/built-in functions
– Adam
Nov 23 '18 at 18:55
1
1
One easy:
print("The new string is:", outputList)
.– Austin
Nov 23 '18 at 18:56
One easy:
print("The new string is:", outputList)
.– Austin
Nov 23 '18 at 18:56
You only print the first item if the string
– Tobias Wilfert
Nov 23 '18 at 18:57
You only print the first item if the string
– Tobias Wilfert
Nov 23 '18 at 18:57
Also outputList[1] is not the amount of characters that have been removed
– Tobias Wilfert
Nov 23 '18 at 18:58
Also outputList[1] is not the amount of characters that have been removed
– Tobias Wilfert
Nov 23 '18 at 18:58
add a comment |
1 Answer
1
active
oldest
votes
Your code should be something like this:
def cleanupstring(S):
counter = 0
lasti = ""
result = ""
for i in S:
if lasti == " " and i == " ":
i = ""
counter += 1
else:
lasti = i
result += i
return result, counter
sentence = input("Enter a string: ")
outputList = cleanupstring(sentence)
print("A total of", outputList[1], "characters have been removed from your string.")
print("The new string is:", outputList[0])
The counter keeps track of how often you remove a character and your [0] and [1] work now the way you want them to.
This is because outputList is now a tuple, the first value at index 0 is now the result and the second value at index 1 is the counter.
thank you that works! I forgot to mention it also must work with sentences containing punctuation like commas and periods. a space after is okay. where can i build in that condition? thanks again
– Funnacho
Nov 23 '18 at 19:16
It doesn't remove any spaces behind a comma is just leaves the one before so 'hello , world' would not be changed.
– Tobias Wilfert
Nov 23 '18 at 19:28
this is because the first space between them is not considered "extra" when it is checking the string?
– Funnacho
Nov 23 '18 at 19:38
@Funnacho yes, the program doesn't check if the spaces are on the correct position it just removes one if there are 2 behind each other.
– Tobias Wilfert
Nov 23 '18 at 19:40
thanks for all the clarification and help, would you be able to send me in the right direction to fix this issue?
– Funnacho
Nov 23 '18 at 19:46
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451708%2fremoving-extra-whitespace-between-words-using-for-loop-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your code should be something like this:
def cleanupstring(S):
counter = 0
lasti = ""
result = ""
for i in S:
if lasti == " " and i == " ":
i = ""
counter += 1
else:
lasti = i
result += i
return result, counter
sentence = input("Enter a string: ")
outputList = cleanupstring(sentence)
print("A total of", outputList[1], "characters have been removed from your string.")
print("The new string is:", outputList[0])
The counter keeps track of how often you remove a character and your [0] and [1] work now the way you want them to.
This is because outputList is now a tuple, the first value at index 0 is now the result and the second value at index 1 is the counter.
thank you that works! I forgot to mention it also must work with sentences containing punctuation like commas and periods. a space after is okay. where can i build in that condition? thanks again
– Funnacho
Nov 23 '18 at 19:16
It doesn't remove any spaces behind a comma is just leaves the one before so 'hello , world' would not be changed.
– Tobias Wilfert
Nov 23 '18 at 19:28
this is because the first space between them is not considered "extra" when it is checking the string?
– Funnacho
Nov 23 '18 at 19:38
@Funnacho yes, the program doesn't check if the spaces are on the correct position it just removes one if there are 2 behind each other.
– Tobias Wilfert
Nov 23 '18 at 19:40
thanks for all the clarification and help, would you be able to send me in the right direction to fix this issue?
– Funnacho
Nov 23 '18 at 19:46
|
show 2 more comments
Your code should be something like this:
def cleanupstring(S):
counter = 0
lasti = ""
result = ""
for i in S:
if lasti == " " and i == " ":
i = ""
counter += 1
else:
lasti = i
result += i
return result, counter
sentence = input("Enter a string: ")
outputList = cleanupstring(sentence)
print("A total of", outputList[1], "characters have been removed from your string.")
print("The new string is:", outputList[0])
The counter keeps track of how often you remove a character and your [0] and [1] work now the way you want them to.
This is because outputList is now a tuple, the first value at index 0 is now the result and the second value at index 1 is the counter.
thank you that works! I forgot to mention it also must work with sentences containing punctuation like commas and periods. a space after is okay. where can i build in that condition? thanks again
– Funnacho
Nov 23 '18 at 19:16
It doesn't remove any spaces behind a comma is just leaves the one before so 'hello , world' would not be changed.
– Tobias Wilfert
Nov 23 '18 at 19:28
this is because the first space between them is not considered "extra" when it is checking the string?
– Funnacho
Nov 23 '18 at 19:38
@Funnacho yes, the program doesn't check if the spaces are on the correct position it just removes one if there are 2 behind each other.
– Tobias Wilfert
Nov 23 '18 at 19:40
thanks for all the clarification and help, would you be able to send me in the right direction to fix this issue?
– Funnacho
Nov 23 '18 at 19:46
|
show 2 more comments
Your code should be something like this:
def cleanupstring(S):
counter = 0
lasti = ""
result = ""
for i in S:
if lasti == " " and i == " ":
i = ""
counter += 1
else:
lasti = i
result += i
return result, counter
sentence = input("Enter a string: ")
outputList = cleanupstring(sentence)
print("A total of", outputList[1], "characters have been removed from your string.")
print("The new string is:", outputList[0])
The counter keeps track of how often you remove a character and your [0] and [1] work now the way you want them to.
This is because outputList is now a tuple, the first value at index 0 is now the result and the second value at index 1 is the counter.
Your code should be something like this:
def cleanupstring(S):
counter = 0
lasti = ""
result = ""
for i in S:
if lasti == " " and i == " ":
i = ""
counter += 1
else:
lasti = i
result += i
return result, counter
sentence = input("Enter a string: ")
outputList = cleanupstring(sentence)
print("A total of", outputList[1], "characters have been removed from your string.")
print("The new string is:", outputList[0])
The counter keeps track of how often you remove a character and your [0] and [1] work now the way you want them to.
This is because outputList is now a tuple, the first value at index 0 is now the result and the second value at index 1 is the counter.
answered Nov 23 '18 at 19:02
Tobias WilfertTobias Wilfert
84531022
84531022
thank you that works! I forgot to mention it also must work with sentences containing punctuation like commas and periods. a space after is okay. where can i build in that condition? thanks again
– Funnacho
Nov 23 '18 at 19:16
It doesn't remove any spaces behind a comma is just leaves the one before so 'hello , world' would not be changed.
– Tobias Wilfert
Nov 23 '18 at 19:28
this is because the first space between them is not considered "extra" when it is checking the string?
– Funnacho
Nov 23 '18 at 19:38
@Funnacho yes, the program doesn't check if the spaces are on the correct position it just removes one if there are 2 behind each other.
– Tobias Wilfert
Nov 23 '18 at 19:40
thanks for all the clarification and help, would you be able to send me in the right direction to fix this issue?
– Funnacho
Nov 23 '18 at 19:46
|
show 2 more comments
thank you that works! I forgot to mention it also must work with sentences containing punctuation like commas and periods. a space after is okay. where can i build in that condition? thanks again
– Funnacho
Nov 23 '18 at 19:16
It doesn't remove any spaces behind a comma is just leaves the one before so 'hello , world' would not be changed.
– Tobias Wilfert
Nov 23 '18 at 19:28
this is because the first space between them is not considered "extra" when it is checking the string?
– Funnacho
Nov 23 '18 at 19:38
@Funnacho yes, the program doesn't check if the spaces are on the correct position it just removes one if there are 2 behind each other.
– Tobias Wilfert
Nov 23 '18 at 19:40
thanks for all the clarification and help, would you be able to send me in the right direction to fix this issue?
– Funnacho
Nov 23 '18 at 19:46
thank you that works! I forgot to mention it also must work with sentences containing punctuation like commas and periods. a space after is okay. where can i build in that condition? thanks again
– Funnacho
Nov 23 '18 at 19:16
thank you that works! I forgot to mention it also must work with sentences containing punctuation like commas and periods. a space after is okay. where can i build in that condition? thanks again
– Funnacho
Nov 23 '18 at 19:16
It doesn't remove any spaces behind a comma is just leaves the one before so 'hello , world' would not be changed.
– Tobias Wilfert
Nov 23 '18 at 19:28
It doesn't remove any spaces behind a comma is just leaves the one before so 'hello , world' would not be changed.
– Tobias Wilfert
Nov 23 '18 at 19:28
this is because the first space between them is not considered "extra" when it is checking the string?
– Funnacho
Nov 23 '18 at 19:38
this is because the first space between them is not considered "extra" when it is checking the string?
– Funnacho
Nov 23 '18 at 19:38
@Funnacho yes, the program doesn't check if the spaces are on the correct position it just removes one if there are 2 behind each other.
– Tobias Wilfert
Nov 23 '18 at 19:40
@Funnacho yes, the program doesn't check if the spaces are on the correct position it just removes one if there are 2 behind each other.
– Tobias Wilfert
Nov 23 '18 at 19:40
thanks for all the clarification and help, would you be able to send me in the right direction to fix this issue?
– Funnacho
Nov 23 '18 at 19:46
thanks for all the clarification and help, would you be able to send me in the right direction to fix this issue?
– Funnacho
Nov 23 '18 at 19:46
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451708%2fremoving-extra-whitespace-between-words-using-for-loop-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Possible duplicate of Cleaning up a string without split/strip/built-in functions
– Adam
Nov 23 '18 at 18:55
1
One easy:
print("The new string is:", outputList)
.– Austin
Nov 23 '18 at 18:56
You only print the first item if the string
– Tobias Wilfert
Nov 23 '18 at 18:57
Also outputList[1] is not the amount of characters that have been removed
– Tobias Wilfert
Nov 23 '18 at 18:58