输出没有whitespce的字符串 [英] To output a string without whitespce

查看:37
本文介绍了输出没有whitespce的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序:

def string_splosion(str):j=1c=len(str)我=0s=''而(i<c):s=s+(str[:i+j])我=我+1印刷print("请输入字符串:")s=raw_input()string_splosion(s)示例输入:代码预期输出:CCoCodCode我的输出:输入一个字符串:代码c co 代码

谁能解释一下如何删除空格并获得 CCoCodCode 的预期输出 Python2.7.12

非常感谢!!!

解决方案

目前,您正在单独打印每个迭代.虽然你可以通过稍微改变逻辑来让你的例子工作,但这对于 Python 魔术来说是一件容易的事:

<预><代码>>>>s = '代码'>>>''.join(s[:i+1] for i in range(len(s)))'CCoCodCode'

My program:

def string_splosion(str):
    j=1
    c=len(str)
    i=0
    s=''
    while(i<c):
        s=s+(str[:i+j])
        i=i+1
        print s


print("Enter a string:")
s=raw_input()
string_splosion(s)

Sample input:Code
Expected output:CCoCodCode
My output:
Enter a string:
code
c co cod code

Could anyone please explain me how to remove the space and get the expected output CCoCodCode for Python2.7.12

Thanks a lot!!!

解决方案

Currently, you are printing each iteration separately. Though you can get your example to work by changing the logic a little bit, this is an easy job for Python magic:

>>> s = 'Code'
>>> ''.join(s[:i+1] for i in range(len(s)))
'CCoCodCode'

这篇关于输出没有whitespce的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆