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;
}







-2















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])









share|improve this question




















  • 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


















-2















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])









share|improve this question




















  • 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














-2












-2








-2








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])









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer
























  • 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












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
});


}
});














draft saved

draft discarded


















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









0














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.






share|improve this answer
























  • 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
















0














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.






share|improve this answer
























  • 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














0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?