Given a string and we have to split into array of characters in Python.

给定一个字符串,我们必须在Python中拆分为字符数组。

将字符串拆分为字符 (Splitting string to characters)

1) Split string using for loop

1)使用for循环分割字符串

Use for loop to convert each character into the list and returns the list/array of the characters.

使用for循环将每个字符转换为列表并返回字符的列表/数组。

Python program to split string into array of characters using for loop

Python程序使用for循环将字符串拆分为字符数组

# Split string using for loop
# function to split string
def split_str(s):
return [ch for ch in s]
# main code
string = "Hello world!"
print("string: ", string)
print("split string...")
print(split_str(string))

Output

输出量

string:  Hello world!
split string...
['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']

2) Split string by converting string to the list (using typecast)

2)通过将字符串转换为列表来分割字符串(使用类型转换)

We can typecast string to the list using list(string) – it will return a list/array of characters.

我们可以使用list(string)字符串类型转换到列表中-它会返回一个字符列表/数组。

Python program to split string into array by typecasting string to list

Python程序通过将字符串类型转换为列表将字符串拆分为数组

# Split string by typecasting
# from string to list
# function to split string
def split_str(s):
return list(s)
# main code
string = "Hello world!"
print("string: ", string)
print("split string...")
print(split_str(string))

string:  Hello world!
split string...
['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']

翻译自: https://www.includehelp.com/python/split-a-string-into-array-of-characters.aspx

在Python中将字符串拆分为字符数组相关推荐

  1. JS将字符串拆分为字符数组

    有多种方法可以将字符串分割成字符数组,我更喜欢使用扩展操作符(...): <!DOCTYPE HTML> <html><head><title>< ...

  2. C语言中将字符串赋值给字符数组的问题

    问不能用赋值语句将一个字符串常量或字符数组直接给一个字符数组赋值." 那么str1="China"是不是错的? char a[ ]='toyou'; 为什么是正确的呢? ...

  3. 在Bash中将字符串拆分为数组

    本文翻译自:Split string into an array in Bash In a Bash script I would like to split a line into pieces a ...

  4. 每天一道LeetCode-----给定字符串s和字符数组words,在s中找到words出现的位置,words内部字符串顺序无要求

    Substring with Concatenation of All Words 原题链接Substring with Concatenation of All Words 题目意思是给定字符串s和 ...

  5. 外挂学习之路(12)--- 用CE搜索字符串和搜索字符数组的区别

    用VS2008写个小工程做测试,得到如下一些结论 代码如下: TCHAR test[50] = TEXT("你好啊"); void CasciiDlg::OnBnClickedBu ...

  6. [转载] 用python统计中文字符数_使用Python统计字符串中各种字符的个数

    参考链接: Python中遍历字符串的单词 Python 统计字符串中各种字符出现的次数 一.提出问题 随机输入一段字符串,包括数字,英文,空格,其他字符,统计这些字符在其中出现的次数 二.难点提示思 ...

  7. [转载] Python从字符串中删除字符

    参考链接: Python | 字符串translate Sometimes we want to remove all occurrences of a character from a string ...

  8. python去除字符串中表情字符

    python去除字符串中表情字符 用mysql存储数据时,"charset=utf8"默认状态下text字段不支持4字节的字符,而表情字符为4字节,如果表情字符非所需数据时可以将其 ...

  9. LeetCode/LintCode 题解丨一周爆刷字符串:旋转字符数组

    描述 给定一个字符数组 s 和一个偏移量,根据偏移量原地旋转字符数组(从左向右旋转). offset >= 0 s 的长度 >= 0 原地旋转意味着需要在函数中更改字符数组 s.你不需要返 ...

最新文章

  1. mysql savepoint 丢失_关于MySQL中savepoint语句使用时所出现的错误
  2. git 基于发布分支的开发
  3. Fiddler抓包使用教程-QuickExec
  4. 工作占用了太多私人时间_职晓|如果工作占用了生活时间,我应不应该辞职?...
  5. Java黑皮书课后题第7章:*7.29(游戏:挑选四张牌)编写一个程序,从一副52张牌中选出4张,然后计算它们的和。Ace King Quee Jack分别表示1、13、12和11,显示得到和24的次
  6. 【SpringBoot零基础案例09】【IEDA 2021.1】SpringBoot将核心配置文件中的自定义配置映射到一个对象
  7. 【遥感物候】1983-2012年时间序列中国地区GIMMS 3g NDVI下载(已进行旋转、格式转换、投影变换和裁剪)
  8. 高度平衡二叉树的构建_数据结构与算法系列(十六)平衡二叉树的构建实现过程演示...
  9. 4005基于邻接表的顶点的删除(C++,附思路)
  10. matlab2c使用c++实现matlab函数系列教程-std函数
  11. 【直观理解】粒子滤波 原理及实现
  12. 【深入浅出imx8企业级开发实战 | 01】imx8qxp yocto工程构建指南
  13. linux怎么限制文件大小,Linux文件系统文件大小限制
  14. java 8中Comparator.comparing和reversed
  15. 便携式洁面仪商城质检报告检验标准是什么
  16. 为什么大厂们 一边裁员,一边招人。。
  17. HTML初识:网页结构、标签、特殊符号、列表
  18. vs code远程连接阿里云(SSH)
  19. scandir、direct的用法
  20. Mac OS X 系统清理

热门文章

  1. 国际站html代码,国际站必须看得懂的HTML代码
  2. 嵌套的json ajax,通过jquery或javascript通过AJAX读取嵌套的JSON并输出到表中
  3. json-schema 可视化编辑器发布了
  4. Web前端开发学习误区,你掉进去了没?
  5. 用asp.net编写冒泡排序法
  6. 第7章 输入/输出系统
  7. jQuary总结11:jQuery插件封装---jQuery封装 手风琴 动画插件
  8. SQL数据库中临时表、临时变量和WITH AS关键词创建“临时表”的区别
  9. Jmeter 场景设计
  10. 程序员的幸福感和颈椎病