Cleaning up a string without split/strip/built-in functions












5















My requirements



Use Python to create a function cleanstring(S) to "clean up" the spaces in a sentence S.




  • The sentence may have extra spaces at the front and/or at the end and/or between words.

  • The subroutine returns a new version of the sentence without the extra spaces.


    • That is, in the new string, the words should be the same but there should be no spaces at the start, only one space between each word and no spaces at the end.




This program is about you writing code to search through a string to find words and so you are not allowed to use the split function in Python.



You can solve this problem with the basic capabilities of the if and while statements and string operations of len and concatentation.



For example: if the input is: " Hello to the world !" then the output should be: "Hello to the world!"



Question



My program deletes more characters in the program than needed.



Input: " Hello World ! "



Output: "HellWorl"



How do I fix the error in my program?



def cleanupstring (S):
newstring = ["", 0]
j = 1
for i in range(len(S) - 1):
if S[i] != " " and S[i+1] != " ":
newstring[0] = newstring[0] + S[i]
else:
newstring[1] = newstring [1] + 1
return newstring

# main program

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

























  • your issue is with if S[i] != " " and S[i+1] != " ". you are checking that neither the current nor next characters are spaces. amongst other issues, it will exclude any single letter words e.g. the string ' a ' will return an empty string when passed through your function

    – aydow
    Nov 22 '18 at 22:25






  • 1





    Apart from if and while and such, you can also solve this with a basic single line regex: return re.sub(r'^s+|(?<=s)s+|s+$', '', string)

    – usr2564301
    Nov 22 '18 at 23:32











  • I am not able to use any built-in functions at all and haven't learned that

    – Mickey Sawkiewicz
    Nov 23 '18 at 1:19


















5















My requirements



Use Python to create a function cleanstring(S) to "clean up" the spaces in a sentence S.




  • The sentence may have extra spaces at the front and/or at the end and/or between words.

  • The subroutine returns a new version of the sentence without the extra spaces.


    • That is, in the new string, the words should be the same but there should be no spaces at the start, only one space between each word and no spaces at the end.




This program is about you writing code to search through a string to find words and so you are not allowed to use the split function in Python.



You can solve this problem with the basic capabilities of the if and while statements and string operations of len and concatentation.



For example: if the input is: " Hello to the world !" then the output should be: "Hello to the world!"



Question



My program deletes more characters in the program than needed.



Input: " Hello World ! "



Output: "HellWorl"



How do I fix the error in my program?



def cleanupstring (S):
newstring = ["", 0]
j = 1
for i in range(len(S) - 1):
if S[i] != " " and S[i+1] != " ":
newstring[0] = newstring[0] + S[i]
else:
newstring[1] = newstring [1] + 1
return newstring

# main program

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

























  • your issue is with if S[i] != " " and S[i+1] != " ". you are checking that neither the current nor next characters are spaces. amongst other issues, it will exclude any single letter words e.g. the string ' a ' will return an empty string when passed through your function

    – aydow
    Nov 22 '18 at 22:25






  • 1





    Apart from if and while and such, you can also solve this with a basic single line regex: return re.sub(r'^s+|(?<=s)s+|s+$', '', string)

    – usr2564301
    Nov 22 '18 at 23:32











  • I am not able to use any built-in functions at all and haven't learned that

    – Mickey Sawkiewicz
    Nov 23 '18 at 1:19
















5












5








5


0






My requirements



Use Python to create a function cleanstring(S) to "clean up" the spaces in a sentence S.




  • The sentence may have extra spaces at the front and/or at the end and/or between words.

  • The subroutine returns a new version of the sentence without the extra spaces.


    • That is, in the new string, the words should be the same but there should be no spaces at the start, only one space between each word and no spaces at the end.




This program is about you writing code to search through a string to find words and so you are not allowed to use the split function in Python.



You can solve this problem with the basic capabilities of the if and while statements and string operations of len and concatentation.



For example: if the input is: " Hello to the world !" then the output should be: "Hello to the world!"



Question



My program deletes more characters in the program than needed.



Input: " Hello World ! "



Output: "HellWorl"



How do I fix the error in my program?



def cleanupstring (S):
newstring = ["", 0]
j = 1
for i in range(len(S) - 1):
if S[i] != " " and S[i+1] != " ":
newstring[0] = newstring[0] + S[i]
else:
newstring[1] = newstring [1] + 1
return newstring

# main program

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
















My requirements



Use Python to create a function cleanstring(S) to "clean up" the spaces in a sentence S.




  • The sentence may have extra spaces at the front and/or at the end and/or between words.

  • The subroutine returns a new version of the sentence without the extra spaces.


    • That is, in the new string, the words should be the same but there should be no spaces at the start, only one space between each word and no spaces at the end.




This program is about you writing code to search through a string to find words and so you are not allowed to use the split function in Python.



You can solve this problem with the basic capabilities of the if and while statements and string operations of len and concatentation.



For example: if the input is: " Hello to the world !" then the output should be: "Hello to the world!"



Question



My program deletes more characters in the program than needed.



Input: " Hello World ! "



Output: "HellWorl"



How do I fix the error in my program?



def cleanupstring (S):
newstring = ["", 0]
j = 1
for i in range(len(S) - 1):
if S[i] != " " and S[i+1] != " ":
newstring[0] = newstring[0] + S[i]
else:
newstring[1] = newstring [1] + 1
return newstring

# main program

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 string indexing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 1:23







Mickey Sawkiewicz

















asked Nov 22 '18 at 22:14









Mickey SawkiewiczMickey Sawkiewicz

263




263













  • your issue is with if S[i] != " " and S[i+1] != " ". you are checking that neither the current nor next characters are spaces. amongst other issues, it will exclude any single letter words e.g. the string ' a ' will return an empty string when passed through your function

    – aydow
    Nov 22 '18 at 22:25






  • 1





    Apart from if and while and such, you can also solve this with a basic single line regex: return re.sub(r'^s+|(?<=s)s+|s+$', '', string)

    – usr2564301
    Nov 22 '18 at 23:32











  • I am not able to use any built-in functions at all and haven't learned that

    – Mickey Sawkiewicz
    Nov 23 '18 at 1:19





















  • your issue is with if S[i] != " " and S[i+1] != " ". you are checking that neither the current nor next characters are spaces. amongst other issues, it will exclude any single letter words e.g. the string ' a ' will return an empty string when passed through your function

    – aydow
    Nov 22 '18 at 22:25






  • 1





    Apart from if and while and such, you can also solve this with a basic single line regex: return re.sub(r'^s+|(?<=s)s+|s+$', '', string)

    – usr2564301
    Nov 22 '18 at 23:32











  • I am not able to use any built-in functions at all and haven't learned that

    – Mickey Sawkiewicz
    Nov 23 '18 at 1:19



















your issue is with if S[i] != " " and S[i+1] != " ". you are checking that neither the current nor next characters are spaces. amongst other issues, it will exclude any single letter words e.g. the string ' a ' will return an empty string when passed through your function

– aydow
Nov 22 '18 at 22:25





your issue is with if S[i] != " " and S[i+1] != " ". you are checking that neither the current nor next characters are spaces. amongst other issues, it will exclude any single letter words e.g. the string ' a ' will return an empty string when passed through your function

– aydow
Nov 22 '18 at 22:25




1




1





Apart from if and while and such, you can also solve this with a basic single line regex: return re.sub(r'^s+|(?<=s)s+|s+$', '', string)

– usr2564301
Nov 22 '18 at 23:32





Apart from if and while and such, you can also solve this with a basic single line regex: return re.sub(r'^s+|(?<=s)s+|s+$', '', string)

– usr2564301
Nov 22 '18 at 23:32













I am not able to use any built-in functions at all and haven't learned that

– Mickey Sawkiewicz
Nov 23 '18 at 1:19







I am not able to use any built-in functions at all and haven't learned that

– Mickey Sawkiewicz
Nov 23 '18 at 1:19














3 Answers
3






active

oldest

votes


















2














Welcome to Stackoverflow. When I started reading I though this was going to be a "please answer my homework" question, but you've actually made a pretty fair effort at solving the problem, so I'm happy to try and help (only you can say whether I actually do).



It's sometimes difficult when you are learning a new language to drop techniques that are much more appropriate in other languages. Doing it character by character you normally just use for c in s rather than incrementing index values like you would in C (though either approach works, index incrementation where not necessary is sometimes regarded as "unpythonic"). Your basic idea seems to be to detect a space followed by another space, otherwise copying characters from the input to the output.



The logic can be simplified by retaining the last character you sent to the output. If it's a space, don't send any more spaces. A loop at the front gets rid of any leading spaces, and since there can be at most one space at the end it can be eliminated easily if present.



I'm not sure why you use a list to keep your results in, as it makes the code much more difficult to understand. If you need to return multiple pieces of information it's much easier to compute them in individual variables and then construct the result in the return statement.



So one desirable modification would be to replace newstring[0] with, say, out_s and newstring[1] with, say count. That will make it a bit clearer what's going on. Then at the end return [out_s, count] if you really need a list. A tuple using return out_s, count would be more usual.



def cleanupstring (s):
out_s = ''
count = 0
last_out = ' '
for c in s:
if c != ' ' or last_out != ' ':
last_out = c
out_s += c
else:
count += 1
if last_out == ' ':
count -= 1
out_s = out_s[:-1]
return out_s, count

# main program

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


Sometimes you just don't have certain pieces of information that would help you to answer the question extremely succinctly. You most likely haven't yet been taught about the strip and replace methods, and so I imagine the following (untested) code



def cleanupstring(s):
out_s = s
while ' ' in out_s:
out_s = out_s.strip().replace(' ', ' ')
return out_s, len(s)-len(out_s)


would be right out.



Also, you can use an "unpacking assignment" to bind the different elements of the function's output directly to names by writing



s, c = cleanupstring(...)


I'm sure you will agree that



print("A total of", c, "characters have been removed from your string.")
print("The new string is:", s)


is rather easier to read. Python values readability so highly because with readable code it's easier to understand the intent of the author. If your code is hard to understand there's a good chance you still have some refactoring to do!






share|improve this answer


























  • This worked but now the issue that I'm having is eliminating the space between the words and punctuation mark, it keeps leaving a space between.

    – Mickey Sawkiewicz
    Nov 23 '18 at 1:41











  • Sure - the problem specification doesn't mention punctuation at all, so I can hardly have been expected to take it into account;-). But the solution is to add conditions that must be true in order to send out even a single space. I'm presuming spaces are OK after punctuation but not before. No time to do more, and this isn't my homework.

    – holdenweb
    Nov 23 '18 at 9:17





















1














If the "space" it's literally spaces rather than whitespace then the following would work:



import re
def clean_string(value):
return re.sub('[ ]{2,}', ' ', value.strip())


If the stripped values contains consecutive spaces then replace with one space.






share|improve this answer


























  • What about 3 consecutive spaces?

    – chriopp
    Nov 22 '18 at 23:52






  • 1





    Three consecutive spaces contains two spaces, so those two would be removed resulting in one space. This highlights a problem: what about four consecutive spaces? All 4 will be removed. I will ponder on this.

    – rikAtee
    Nov 22 '18 at 23:56













  • There we go (updated)

    – rikAtee
    Nov 23 '18 at 0:02



















0














My approach would be to keep the last character available and make the decision whether it is a space or not:



def cleanupstring (S):
newstring = ["", 0]
last_character = ' ' # catch initial spaces
for i in range(len(S)-1):
char = S[i]
if char is ' ' and last_character is ' ':
continue # ignore
else:
last_character = char
newstring [0] = newstring[0] + char

return newstring





share|improve this answer

























    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%2f53438554%2fcleaning-up-a-string-without-split-strip-built-in-functions%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Welcome to Stackoverflow. When I started reading I though this was going to be a "please answer my homework" question, but you've actually made a pretty fair effort at solving the problem, so I'm happy to try and help (only you can say whether I actually do).



    It's sometimes difficult when you are learning a new language to drop techniques that are much more appropriate in other languages. Doing it character by character you normally just use for c in s rather than incrementing index values like you would in C (though either approach works, index incrementation where not necessary is sometimes regarded as "unpythonic"). Your basic idea seems to be to detect a space followed by another space, otherwise copying characters from the input to the output.



    The logic can be simplified by retaining the last character you sent to the output. If it's a space, don't send any more spaces. A loop at the front gets rid of any leading spaces, and since there can be at most one space at the end it can be eliminated easily if present.



    I'm not sure why you use a list to keep your results in, as it makes the code much more difficult to understand. If you need to return multiple pieces of information it's much easier to compute them in individual variables and then construct the result in the return statement.



    So one desirable modification would be to replace newstring[0] with, say, out_s and newstring[1] with, say count. That will make it a bit clearer what's going on. Then at the end return [out_s, count] if you really need a list. A tuple using return out_s, count would be more usual.



    def cleanupstring (s):
    out_s = ''
    count = 0
    last_out = ' '
    for c in s:
    if c != ' ' or last_out != ' ':
    last_out = c
    out_s += c
    else:
    count += 1
    if last_out == ' ':
    count -= 1
    out_s = out_s[:-1]
    return out_s, count

    # main program

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


    Sometimes you just don't have certain pieces of information that would help you to answer the question extremely succinctly. You most likely haven't yet been taught about the strip and replace methods, and so I imagine the following (untested) code



    def cleanupstring(s):
    out_s = s
    while ' ' in out_s:
    out_s = out_s.strip().replace(' ', ' ')
    return out_s, len(s)-len(out_s)


    would be right out.



    Also, you can use an "unpacking assignment" to bind the different elements of the function's output directly to names by writing



    s, c = cleanupstring(...)


    I'm sure you will agree that



    print("A total of", c, "characters have been removed from your string.")
    print("The new string is:", s)


    is rather easier to read. Python values readability so highly because with readable code it's easier to understand the intent of the author. If your code is hard to understand there's a good chance you still have some refactoring to do!






    share|improve this answer


























    • This worked but now the issue that I'm having is eliminating the space between the words and punctuation mark, it keeps leaving a space between.

      – Mickey Sawkiewicz
      Nov 23 '18 at 1:41











    • Sure - the problem specification doesn't mention punctuation at all, so I can hardly have been expected to take it into account;-). But the solution is to add conditions that must be true in order to send out even a single space. I'm presuming spaces are OK after punctuation but not before. No time to do more, and this isn't my homework.

      – holdenweb
      Nov 23 '18 at 9:17


















    2














    Welcome to Stackoverflow. When I started reading I though this was going to be a "please answer my homework" question, but you've actually made a pretty fair effort at solving the problem, so I'm happy to try and help (only you can say whether I actually do).



    It's sometimes difficult when you are learning a new language to drop techniques that are much more appropriate in other languages. Doing it character by character you normally just use for c in s rather than incrementing index values like you would in C (though either approach works, index incrementation where not necessary is sometimes regarded as "unpythonic"). Your basic idea seems to be to detect a space followed by another space, otherwise copying characters from the input to the output.



    The logic can be simplified by retaining the last character you sent to the output. If it's a space, don't send any more spaces. A loop at the front gets rid of any leading spaces, and since there can be at most one space at the end it can be eliminated easily if present.



    I'm not sure why you use a list to keep your results in, as it makes the code much more difficult to understand. If you need to return multiple pieces of information it's much easier to compute them in individual variables and then construct the result in the return statement.



    So one desirable modification would be to replace newstring[0] with, say, out_s and newstring[1] with, say count. That will make it a bit clearer what's going on. Then at the end return [out_s, count] if you really need a list. A tuple using return out_s, count would be more usual.



    def cleanupstring (s):
    out_s = ''
    count = 0
    last_out = ' '
    for c in s:
    if c != ' ' or last_out != ' ':
    last_out = c
    out_s += c
    else:
    count += 1
    if last_out == ' ':
    count -= 1
    out_s = out_s[:-1]
    return out_s, count

    # main program

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


    Sometimes you just don't have certain pieces of information that would help you to answer the question extremely succinctly. You most likely haven't yet been taught about the strip and replace methods, and so I imagine the following (untested) code



    def cleanupstring(s):
    out_s = s
    while ' ' in out_s:
    out_s = out_s.strip().replace(' ', ' ')
    return out_s, len(s)-len(out_s)


    would be right out.



    Also, you can use an "unpacking assignment" to bind the different elements of the function's output directly to names by writing



    s, c = cleanupstring(...)


    I'm sure you will agree that



    print("A total of", c, "characters have been removed from your string.")
    print("The new string is:", s)


    is rather easier to read. Python values readability so highly because with readable code it's easier to understand the intent of the author. If your code is hard to understand there's a good chance you still have some refactoring to do!






    share|improve this answer


























    • This worked but now the issue that I'm having is eliminating the space between the words and punctuation mark, it keeps leaving a space between.

      – Mickey Sawkiewicz
      Nov 23 '18 at 1:41











    • Sure - the problem specification doesn't mention punctuation at all, so I can hardly have been expected to take it into account;-). But the solution is to add conditions that must be true in order to send out even a single space. I'm presuming spaces are OK after punctuation but not before. No time to do more, and this isn't my homework.

      – holdenweb
      Nov 23 '18 at 9:17
















    2












    2








    2







    Welcome to Stackoverflow. When I started reading I though this was going to be a "please answer my homework" question, but you've actually made a pretty fair effort at solving the problem, so I'm happy to try and help (only you can say whether I actually do).



    It's sometimes difficult when you are learning a new language to drop techniques that are much more appropriate in other languages. Doing it character by character you normally just use for c in s rather than incrementing index values like you would in C (though either approach works, index incrementation where not necessary is sometimes regarded as "unpythonic"). Your basic idea seems to be to detect a space followed by another space, otherwise copying characters from the input to the output.



    The logic can be simplified by retaining the last character you sent to the output. If it's a space, don't send any more spaces. A loop at the front gets rid of any leading spaces, and since there can be at most one space at the end it can be eliminated easily if present.



    I'm not sure why you use a list to keep your results in, as it makes the code much more difficult to understand. If you need to return multiple pieces of information it's much easier to compute them in individual variables and then construct the result in the return statement.



    So one desirable modification would be to replace newstring[0] with, say, out_s and newstring[1] with, say count. That will make it a bit clearer what's going on. Then at the end return [out_s, count] if you really need a list. A tuple using return out_s, count would be more usual.



    def cleanupstring (s):
    out_s = ''
    count = 0
    last_out = ' '
    for c in s:
    if c != ' ' or last_out != ' ':
    last_out = c
    out_s += c
    else:
    count += 1
    if last_out == ' ':
    count -= 1
    out_s = out_s[:-1]
    return out_s, count

    # main program

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


    Sometimes you just don't have certain pieces of information that would help you to answer the question extremely succinctly. You most likely haven't yet been taught about the strip and replace methods, and so I imagine the following (untested) code



    def cleanupstring(s):
    out_s = s
    while ' ' in out_s:
    out_s = out_s.strip().replace(' ', ' ')
    return out_s, len(s)-len(out_s)


    would be right out.



    Also, you can use an "unpacking assignment" to bind the different elements of the function's output directly to names by writing



    s, c = cleanupstring(...)


    I'm sure you will agree that



    print("A total of", c, "characters have been removed from your string.")
    print("The new string is:", s)


    is rather easier to read. Python values readability so highly because with readable code it's easier to understand the intent of the author. If your code is hard to understand there's a good chance you still have some refactoring to do!






    share|improve this answer















    Welcome to Stackoverflow. When I started reading I though this was going to be a "please answer my homework" question, but you've actually made a pretty fair effort at solving the problem, so I'm happy to try and help (only you can say whether I actually do).



    It's sometimes difficult when you are learning a new language to drop techniques that are much more appropriate in other languages. Doing it character by character you normally just use for c in s rather than incrementing index values like you would in C (though either approach works, index incrementation where not necessary is sometimes regarded as "unpythonic"). Your basic idea seems to be to detect a space followed by another space, otherwise copying characters from the input to the output.



    The logic can be simplified by retaining the last character you sent to the output. If it's a space, don't send any more spaces. A loop at the front gets rid of any leading spaces, and since there can be at most one space at the end it can be eliminated easily if present.



    I'm not sure why you use a list to keep your results in, as it makes the code much more difficult to understand. If you need to return multiple pieces of information it's much easier to compute them in individual variables and then construct the result in the return statement.



    So one desirable modification would be to replace newstring[0] with, say, out_s and newstring[1] with, say count. That will make it a bit clearer what's going on. Then at the end return [out_s, count] if you really need a list. A tuple using return out_s, count would be more usual.



    def cleanupstring (s):
    out_s = ''
    count = 0
    last_out = ' '
    for c in s:
    if c != ' ' or last_out != ' ':
    last_out = c
    out_s += c
    else:
    count += 1
    if last_out == ' ':
    count -= 1
    out_s = out_s[:-1]
    return out_s, count

    # main program

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


    Sometimes you just don't have certain pieces of information that would help you to answer the question extremely succinctly. You most likely haven't yet been taught about the strip and replace methods, and so I imagine the following (untested) code



    def cleanupstring(s):
    out_s = s
    while ' ' in out_s:
    out_s = out_s.strip().replace(' ', ' ')
    return out_s, len(s)-len(out_s)


    would be right out.



    Also, you can use an "unpacking assignment" to bind the different elements of the function's output directly to names by writing



    s, c = cleanupstring(...)


    I'm sure you will agree that



    print("A total of", c, "characters have been removed from your string.")
    print("The new string is:", s)


    is rather easier to read. Python values readability so highly because with readable code it's easier to understand the intent of the author. If your code is hard to understand there's a good chance you still have some refactoring to do!







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 9:49

























    answered Nov 22 '18 at 22:47









    holdenwebholdenweb

    15.3k43151




    15.3k43151













    • This worked but now the issue that I'm having is eliminating the space between the words and punctuation mark, it keeps leaving a space between.

      – Mickey Sawkiewicz
      Nov 23 '18 at 1:41











    • Sure - the problem specification doesn't mention punctuation at all, so I can hardly have been expected to take it into account;-). But the solution is to add conditions that must be true in order to send out even a single space. I'm presuming spaces are OK after punctuation but not before. No time to do more, and this isn't my homework.

      – holdenweb
      Nov 23 '18 at 9:17





















    • This worked but now the issue that I'm having is eliminating the space between the words and punctuation mark, it keeps leaving a space between.

      – Mickey Sawkiewicz
      Nov 23 '18 at 1:41











    • Sure - the problem specification doesn't mention punctuation at all, so I can hardly have been expected to take it into account;-). But the solution is to add conditions that must be true in order to send out even a single space. I'm presuming spaces are OK after punctuation but not before. No time to do more, and this isn't my homework.

      – holdenweb
      Nov 23 '18 at 9:17



















    This worked but now the issue that I'm having is eliminating the space between the words and punctuation mark, it keeps leaving a space between.

    – Mickey Sawkiewicz
    Nov 23 '18 at 1:41





    This worked but now the issue that I'm having is eliminating the space between the words and punctuation mark, it keeps leaving a space between.

    – Mickey Sawkiewicz
    Nov 23 '18 at 1:41













    Sure - the problem specification doesn't mention punctuation at all, so I can hardly have been expected to take it into account;-). But the solution is to add conditions that must be true in order to send out even a single space. I'm presuming spaces are OK after punctuation but not before. No time to do more, and this isn't my homework.

    – holdenweb
    Nov 23 '18 at 9:17







    Sure - the problem specification doesn't mention punctuation at all, so I can hardly have been expected to take it into account;-). But the solution is to add conditions that must be true in order to send out even a single space. I'm presuming spaces are OK after punctuation but not before. No time to do more, and this isn't my homework.

    – holdenweb
    Nov 23 '18 at 9:17















    1














    If the "space" it's literally spaces rather than whitespace then the following would work:



    import re
    def clean_string(value):
    return re.sub('[ ]{2,}', ' ', value.strip())


    If the stripped values contains consecutive spaces then replace with one space.






    share|improve this answer


























    • What about 3 consecutive spaces?

      – chriopp
      Nov 22 '18 at 23:52






    • 1





      Three consecutive spaces contains two spaces, so those two would be removed resulting in one space. This highlights a problem: what about four consecutive spaces? All 4 will be removed. I will ponder on this.

      – rikAtee
      Nov 22 '18 at 23:56













    • There we go (updated)

      – rikAtee
      Nov 23 '18 at 0:02
















    1














    If the "space" it's literally spaces rather than whitespace then the following would work:



    import re
    def clean_string(value):
    return re.sub('[ ]{2,}', ' ', value.strip())


    If the stripped values contains consecutive spaces then replace with one space.






    share|improve this answer


























    • What about 3 consecutive spaces?

      – chriopp
      Nov 22 '18 at 23:52






    • 1





      Three consecutive spaces contains two spaces, so those two would be removed resulting in one space. This highlights a problem: what about four consecutive spaces? All 4 will be removed. I will ponder on this.

      – rikAtee
      Nov 22 '18 at 23:56













    • There we go (updated)

      – rikAtee
      Nov 23 '18 at 0:02














    1












    1








    1







    If the "space" it's literally spaces rather than whitespace then the following would work:



    import re
    def clean_string(value):
    return re.sub('[ ]{2,}', ' ', value.strip())


    If the stripped values contains consecutive spaces then replace with one space.






    share|improve this answer















    If the "space" it's literally spaces rather than whitespace then the following would work:



    import re
    def clean_string(value):
    return re.sub('[ ]{2,}', ' ', value.strip())


    If the stripped values contains consecutive spaces then replace with one space.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 0:02

























    answered Nov 22 '18 at 23:45









    rikAteerikAtee

    4,83552958




    4,83552958













    • What about 3 consecutive spaces?

      – chriopp
      Nov 22 '18 at 23:52






    • 1





      Three consecutive spaces contains two spaces, so those two would be removed resulting in one space. This highlights a problem: what about four consecutive spaces? All 4 will be removed. I will ponder on this.

      – rikAtee
      Nov 22 '18 at 23:56













    • There we go (updated)

      – rikAtee
      Nov 23 '18 at 0:02



















    • What about 3 consecutive spaces?

      – chriopp
      Nov 22 '18 at 23:52






    • 1





      Three consecutive spaces contains two spaces, so those two would be removed resulting in one space. This highlights a problem: what about four consecutive spaces? All 4 will be removed. I will ponder on this.

      – rikAtee
      Nov 22 '18 at 23:56













    • There we go (updated)

      – rikAtee
      Nov 23 '18 at 0:02

















    What about 3 consecutive spaces?

    – chriopp
    Nov 22 '18 at 23:52





    What about 3 consecutive spaces?

    – chriopp
    Nov 22 '18 at 23:52




    1




    1





    Three consecutive spaces contains two spaces, so those two would be removed resulting in one space. This highlights a problem: what about four consecutive spaces? All 4 will be removed. I will ponder on this.

    – rikAtee
    Nov 22 '18 at 23:56







    Three consecutive spaces contains two spaces, so those two would be removed resulting in one space. This highlights a problem: what about four consecutive spaces? All 4 will be removed. I will ponder on this.

    – rikAtee
    Nov 22 '18 at 23:56















    There we go (updated)

    – rikAtee
    Nov 23 '18 at 0:02





    There we go (updated)

    – rikAtee
    Nov 23 '18 at 0:02











    0














    My approach would be to keep the last character available and make the decision whether it is a space or not:



    def cleanupstring (S):
    newstring = ["", 0]
    last_character = ' ' # catch initial spaces
    for i in range(len(S)-1):
    char = S[i]
    if char is ' ' and last_character is ' ':
    continue # ignore
    else:
    last_character = char
    newstring [0] = newstring[0] + char

    return newstring





    share|improve this answer






























      0














      My approach would be to keep the last character available and make the decision whether it is a space or not:



      def cleanupstring (S):
      newstring = ["", 0]
      last_character = ' ' # catch initial spaces
      for i in range(len(S)-1):
      char = S[i]
      if char is ' ' and last_character is ' ':
      continue # ignore
      else:
      last_character = char
      newstring [0] = newstring[0] + char

      return newstring





      share|improve this answer




























        0












        0








        0







        My approach would be to keep the last character available and make the decision whether it is a space or not:



        def cleanupstring (S):
        newstring = ["", 0]
        last_character = ' ' # catch initial spaces
        for i in range(len(S)-1):
        char = S[i]
        if char is ' ' and last_character is ' ':
        continue # ignore
        else:
        last_character = char
        newstring [0] = newstring[0] + char

        return newstring





        share|improve this answer















        My approach would be to keep the last character available and make the decision whether it is a space or not:



        def cleanupstring (S):
        newstring = ["", 0]
        last_character = ' ' # catch initial spaces
        for i in range(len(S)-1):
        char = S[i]
        if char is ' ' and last_character is ' ':
        continue # ignore
        else:
        last_character = char
        newstring [0] = newstring[0] + char

        return newstring






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 23:33

























        answered Nov 22 '18 at 22:42









        chrioppchriopp

        60249




        60249






























            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%2f53438554%2fcleaning-up-a-string-without-split-strip-built-in-functions%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

            Wiesbaden

            Marschland

            Dieringhausen