Python-在不使用内置方法的情况下删除字符串中的空间

Python-在不使用内置方法的情况下删除字符串中的空间,python,string,Python,String,我正在尝试创建/获取一个不包含空格的新字符串 比如说, string1 = "my music is good" 应该成为 joinedstring = "mymusicisgood" #this is the one i want 起初,我可以通过使用内置的string方法来实现这一点 string1 = "my music is good" joinedstring = string1.replace(" ","") 但是现在我试图在没有任何字符串方法的情况下获得相同的结果,这意味

我正在尝试创建/获取一个不包含空格的新字符串 比如说,

string1 = "my music is good"
应该成为

joinedstring = "mymusicisgood"   #this is the one i want
起初,我可以通过使用内置的string方法来实现这一点

string1 = "my music is good"
joinedstring = string1.replace(" ","")
但是现在我试图在没有任何字符串方法的情况下获得相同的结果,这意味着我必须创建自己的函数,我的想法是创建一个循环,搜索字符串中“空格”的索引,然后尝试删除索引以生成新字符串。
但是问题来了,字符串是不可变的,所以删除say
del string[index]
不起作用。请帮忙

无需导入字符串来进行替换。是任何字符串上的方法:

>>> string1 = "my music is good"
>>> joinedstring = string1.replace(" ","")
>>> joinedstring
'mymusicisgood'

如果你需要自己“编程”内置函数,看起来很像学校作业。 一个非常简单的函数如下所示:

def no_spaces(string1):
    helpstring=""
    for a in string1:
      if a == " ":
         pass
      else:
         helpstring=helpstring+a
    return helpstring

另一种解决方案是使用带过滤器的列表理解(如果
if
部分):


下面是一个可以从代码中调用的工作函数:

def remove_space(old_string):
    new_string = ""
    for letter in old_string:
        if not letter == " ":
            new_string += letter
        else:
            pass
    print new_string # put here return instead of print if you want

if __name__ == '__main__':
    remove_space("there was a time")

希望这就是您正在寻找的@user3564573

谢谢,现在已经有一个月的编程时间了,我想我必须再次打开我的书才能找到它。行
导入字符串
没有使用,可以删除
def remove_space(old_string):
    new_string = ""
    for letter in old_string:
        if not letter == " ":
            new_string += letter
        else:
            pass
    print new_string # put here return instead of print if you want

if __name__ == '__main__':
    remove_space("there was a time")