Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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_Printing_Output - Fatal编程技术网

Python 如何打印字符串中每个单词的第一个字母?

Python 如何打印字符串中每个单词的第一个字母?,python,string,printing,output,Python,String,Printing,Output,我是Python新手,我想尝试更多的字符串操作 我想打印声明中每个单词的第一个字母 'I like sweet and savoury food' 。。。因此,输出如下所示: 'I l s a s f' 一个简单的“pythonic”方法是 print(' '.join(word[0] for word in sentence.split())) 像这样的for循环称为列表理解。你可以详细写出来: split = sentence.split() result = '' for word

我是Python新手,我想尝试更多的字符串操作

我想打印声明中每个单词的第一个字母

'I like sweet and savoury food'
。。。因此,输出如下所示:

'I l s a s f'
一个简单的“pythonic”方法是

print(' '.join(word[0] for word in sentence.split()))

像这样的
for
循环称为列表理解。你可以详细写出来:

split = sentence.split()
result = ''
for word in split:
    result += word[0] + ' '
print(result.strip()) # strip to get rid of trailing space    
一个简单的“pythonic”方法是

print(' '.join(word[0] for word in sentence.split()))

像这样的
for
循环称为列表理解。你可以详细写出来:

split = sentence.split()
result = ''
for word in split:
    result += word[0] + ' '
print(result.strip()) # strip to get rid of trailing space    

是的,这很有用!你会如何在每个字母之间留出一个空格?“告诉我如何解决这个编码问题”是。你需要做一个调查,然后问一些具体的问题(如果必要的话)。是的,这是非常有用的!你会如何在每个字母之间留出一个空格?“告诉我如何解决这个编码问题”是。我们希望你先做一个简单的回答,然后问一些具体的问题(如果需要的话)。谢谢你的帮助!谢谢你的帮助!