[英]add space between character of string in python use “for” loops in python
I write this code but it does not work:我写了这段代码,但它不起作用:
a=input('word: ')
for i in a:
print(i,'')
what is the problem?问题是什么?
By default, print
inserts a linebreak at the end of the string.默认情况下, print
在字符串末尾插入换行符。 You have to change it:你必须改变它:
a=input('word: ')
for i in a:
print(i,end=' ')
Output for Input Hello
: Output 用于输入Hello
:
H ello
You should provide the expected output because now we have to assume what it is you are trying to achieve.您应该提供预期的 output 因为现在我们必须假设您要达到的目标。
When using "abc" as input I am now assuming you want当使用“abc”作为输入时,我现在假设你想要
a b c
instead of代替
a
b
c
The problem here is, with every loop you call the 'print' function which starts a new line automatically when you should format your output correctly before calling print.这里的问题是,每次循环调用“打印” function 时,当您应该在调用打印之前正确格式化 output 时,它会自动开始一个新行。 Try this:尝试这个:
a=input('word: ')
s = ""
for i in a:
s += i + " "
print(s)
Try doing this尝试这样做
a=input('word: ')
def func(a):
lst = list(a)
return " ".join(lst)
print(func(a))
Actually you didn't add space.实际上你没有添加空间。 Try this:尝试这个:
a=input('word: ')
for i in a:
print(i,' ')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.