-2

I have this function:

def cesar():
    n = int(raw_input("Introdueix la teva clau: "))
    f = raw_input("Introdueix la frase a xifrar: ")
    for ch in f:
        xifrat = int(ord(ch)+int(n))
        textxifrat = chr(xifrat)

        print textxifrat,
cesar()

And I want to remove the spaces from the output or the string. Problem is if I use the .replace Python says that it can't be applied to a str and I don't know any other way to remove those spaces.

The idea is that I want something like this: M t q f % v z j % f x j to become this: Mtqf%vzj%fxj

Mp0int
  • 18,172
  • 15
  • 83
  • 114

5 Answers5

1

Why you can't use .replace()?

>>> a = 'M t q f % v z j % f x j'
>>> a.replace(' ', '')
'Mtqf%vzj%fxj'
>>> 
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
0

You may simply join the string elements with empty string "" after splitting the whole string on white spaces.:

>>> "".join("M t q f % v z j % f x j".split())
>>> Mtqf%vzj%fxj
ZdaR
  • 22,343
  • 7
  • 66
  • 87
0

First split the raw input.

tmp = f.split()

Then join it

result = "".join(tmp)
Tamim Addari
  • 7,591
  • 9
  • 40
  • 59
-1

Give this a try?

def cesar():
    n = int(raw_input("Introdueix la teva clau: "))
    f = raw_input("Introdueix la frase a xifrar: ")

    print f.replace(' ', '')

replace for python 2

Darnell W.
  • 374
  • 2
  • 13
-1

This can be done by using replace().

   str ="%5 65 hfhf ? fgdgdf ";
   str = str.replace(' ','')
   print(str)

This code will remove blank spaces from the string.

dinesh.kumar
  • 167
  • 1
  • 10
  • 3
    The only real difference between this and @Kevin's earlier answer is that this one masks the `str` built-in function. – TigerhawkT3 Sep 30 '15 at 08:16