Python 如何打印字符串中每个单词的第一个字母?
我是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
'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
是的,这很有用!你会如何在每个字母之间留出一个空格?“告诉我如何解决这个编码问题”是。你需要做一个调查,然后问一些具体的问题(如果必要的话)。是的,这是非常有用的!你会如何在每个字母之间留出一个空格?“告诉我如何解决这个编码问题”是。我们希望你先做一个简单的回答,然后问一些具体的问题(如果需要的话)。谢谢你的帮助!谢谢你的帮助!