Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除字符串中的空格_Python_String_Spaces - Fatal编程技术网

Python 删除字符串中的空格

Python 删除字符串中的空格,python,string,spaces,Python,String,Spaces,我有这个功能: 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() 我想

我有这个功能:

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()
我想从输出或字符串中删除空格。 问题是,如果我使用.replace Python,它会说它不能应用于str,我不知道还有什么其他方法可以删除这些空格


我的想法是,我想要这样的东西:Mtqf%vzj%fxj变成这样:Mtqf%vzj%fxj

在空白处分割整个字符串后,您可以简单地用空字符串连接字符串元素:

>>> "".join("M t q f % v z j % f x j".split())
>>> Mtqf%vzj%fxj

首先分割原始输入

tmp = f.split()
那就加入吧

result = "".join(tmp)
为什么不能使用。替换

试试这个

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

    print f.replace(' ', '')

这可以通过使用replace来完成

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

此代码将删除字符串中的空格。

.joinM t q f%v z j%f x j.splitType在Google的Python中删除字符串中的空格。替换不能应用于字符串吗?什么?你能告诉我你是怎么申请的吗。有奇怪的事情,很奇怪,或者你在打错字什么的。你能给我们一个回溯吗?你的答案和我的答案有什么不同吗?不,对不起,我没注意到。@SpeedArt先生,你能提供一个追踪吗?这和@Kevin之前的答案唯一的区别是,这一个掩盖了str内置函数。
   str ="%5 65 hfhf ? fgdgdf ";
   str = str.replace(' ','')
   print(str)