Switch string to high box and remove blanks

Asked

Viewed 2,226 times

5

I’m trying to make a program in Python 3 that should turn the typed text into upper box and remove blanks. I would like to know what is wrong with my code since it is not eliminating the whitespace. I have tested two codes.

  • First code:

    print ("-"*50)
    print ("-"*50)
    
    frase = input("\nEscreva uma frase: ")
    
    frase = frase.upper()
    frase = frase.strip()
    
    print ("\n",frase)
    
    
    print ("-"*50)
    print ("-"*50)
    
  • Second Code

    print ("-"*50)
    print ("-"*50)
    
    frase = input("\nEscreva uma frase: ")
    
    frase = frase.upper()
    frase = frase.replace('','')
    
    print ("\n",frase)
    
    print ("-"*50)
    print ("-"*50)
    

2 answers

7


The first only takes spaces from the beginning and end, the middle would be the second code, but it is not changing anything, the code says to trade anything for nothing, that is, it does not move, it has to be space for nothing:

frase = input("Escreva uma frase: ")
frase = frase.upper().replace(' ','')
print("\n", frase)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

But if it is an exercise perhaps the goal would be to do in hand, character by character without using ready function. What could even have more performance eventually because in the current form has to stir twice in the same string, with a single loop you can swap the character and remove space at the same time. So:

frase = input("Escreva uma frase: ")
fraseNova = ""
for chr in frase:
    if chr != " ":
        fraseNova += chr.upper()
print(fraseNova)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

3

The method str.strip() removes characters from the beginning and the end of a string only.

string = "     a     b     ".strip()
print(string);  # 'a     b'

Already the method str.replace() takes as first parameter a search string and as second parameter a substitution string. The method will replace all occurrences* of the search string for the replacement string.

string = "     a     b     ".replace(' ', '-')
print(string);  # "-----a-----b-----"

* the 3rd parameter defines the maximum amount of substitution the method will do. If not specified, all occurrences will be replaced. [Docs]

So your second code is wrong just because you’re asking to search for nothing ('') and replacing with nothing (''). When what you want is to search for spaces (' ') and replace them with nothing ('').

As Maniero has already responded, his code would look something like:

frase = input('...')
frase = frase.upper().replace(' ','')
print(frase)
  • Thank you. helped me a lot.

  • I did not put in the answer not to complicate unnecessarily, but if you enter the documentation of strip you will see that you can also pass parameters to the method and change its behavior.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.