如何在python中将字符串拆分为字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25698696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:30:40  来源:igfitidea点击:

How to split a string into characters in python?

pythonstringlistcharacter

提问by Shoryu

I know that:

我知道:

print(list('Hello'))

will print

将打印

['H', 'e', 'l', 'l', 'o']

and I know that

我知道

print(list('Hello world!'))

will print

将打印

['Hello', 'world!']

What syntax would be easiest to get:

什么语法最容易获得:

['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']

回答by falsetru

list('Hello world!')gives what you want, not ['Hello', 'world!'].

list('Hello world!')给你想要的,而不是['Hello', 'world!'].

>>> print(list('Hello world!'))
['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']

I think you confused the output of str.split:

我认为您混淆了以下输出str.split

>>> print('Hello world!'.split())
['Hello', 'world!']

回答by xiyurui

work under python 3.6

在 python 3.6 下工作

 a = "Hello world!"
 listresp = list(map(list, a))
 listff =[]
 print (listresp)
 for charersp in listresp:
     for charfinnal in charersp:
         listff.append(charfinnal)
 print (listff)


 ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']