PYTHON中如何将一个单词分割

例如:Hello
存储在list中【‘H’,‘e’,‘l’,l‘,'o’】,
怎么用循环实现?

>>> astring = 'Hello'

# 最直接的方法
>>> alist = list(astring)
>>> alist
['H', 'e', 'l', 'l', 'o']

# 列表推导
>>> alist = [ch for ch in astring]
>>> alist
['H', 'e', 'l', 'l', 'o']

# 循环法
>>> alist = []
>>> for ch in astring:
...     alist.append(ch)
... 
>>> alist
['H', 'e', 'l', 'l', 'o']

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-02-17
[python] view plaincopy
str="a|and|hello|||ab"
alist = str.split('|')
print alist

str="a hello{这里换成5个空格}world{这里换成3个空格}"
alist=str.split(' ')
print alist

统计英文单词的个数的python代码

[python] view plaincopy
# -*- coding: utf-8 -*-
import os,sys
info = os.getcwd() #获取当前文件名称
fin = open(u'c:/a.txt')

info = fin.read()
alist = info.split(' ') # 将文章按照空格划分开

fout = open(u'c:/count.txt', 'w')
fout.write('\n'.join(alist)) # 可以通过文本文件的行号同样看到效果
##fout.write('%s' % alist)
fout.close()

allen = len(alist) # 总的单词数
nulen = alist.count('') # 空格的数量
print "words' number is",allen
print "null number is",nulen
print "poor words number is", allen-nulen # 实际的单词数目

fin.close()
第2个回答  2014-03-03
str_1 = 'hello'
lst_1 = []
for i in str_1:
lst_1.append(i)
print(lst_1)
----------------------
['h', 'e', 'l', 'l', 'o']
[Finished in 0.3s]

第3个回答  2014-03-03
那要看你分割成什么形式,