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

Python 编写显示名称首字母和末字母的表达式

Python 编写显示名称首字母和末字母的表达式,python,Python,我正在努力掌握如何让程序自动获取空格后出现的字母。您需要从每个单词中获取第一个字母 看看 从文档中: s= 'Jon Wayne' x= s.index(' ') value=s[1]+x print(value) 下面是一个例子: Strings can be indexed (subscripted), with the first character having index 0 >>> name = 'Jon Wayne' >>> initial

我正在努力掌握如何让程序自动获取空格后出现的字母。

您需要从每个单词中获取第一个字母

看看

从文档中:

s= 'Jon Wayne'
x= s.index(' ')
value=s[1]+x
print(value)
下面是一个例子:

Strings can be indexed (subscripted), with the first character having index 0
>>> name = 'Jon Wayne' 
>>> initial = ''.join((word[0] for word in name.split()))
>>> initial
'JW'
>>> 
name = "Jon Wayne"
bits = name.split(" ")   # ['Jon', 'Wayne']
for bit in bits:
    print bit[0]         #  J W