Python将字符串转换为列表

Python将字符串转换为列表WecanconvertastringtolistinPythonusingsplit()function.我们可以使用split()函数将字符串转换为Python中的列表。PythonStringsplit()functionsyntaxis:Python字符串split()函数语法为:str.split(sep=None,maxsplit=-1)…

大家好,又见面了,我是你们的朋友全栈君。

We can convert a string to list in Python using split() function.

我们可以使用split()函数将字符串转换为Python中的列表。

Python String split() function syntax is:

Python字符串split()函数语法为:

str.split(sep=None, maxsplit=-1)

Python将字符串转换为列表 (Python Convert String to List)

Let’s look at a simple example where we want to convert a string to list of words i.e. split it with the separator as white spaces.

让我们看一个简单的示例,在此示例中,我们要将字符串转换为单词列表,即使用分隔符将其分割为空白。

s = 'Welcome To JournalDev'
print(f'List of Words ={s.split()}')

Output: List of Words =['Welcome', 'To', 'JournalDev']

输出: List of Words =['Welcome', 'To', 'JournalDev']

If we want to split a string to list based on whitespaces, then we don’t need to provide any separator to the split() function. Also, any leading and trailing whitespaces are trimmed before the string is split into a list of words. So the output will remain same for string s = ' Welcome To JournalDev ' too.

如果我们想将字符串拆分为基于空格的列表,则无需为split()函数提供任何分隔符。 同样,在将字符串拆分为单词列表之前,将修剪所有前导和尾随空格。 因此,对于字符串s = ' Welcome To JournalDev ' ,输出也将保持相同。

Let’s look at another example where we have CSV data into a string and we will convert it to the list of items.

让我们看另一个示例,其中将CSV数据转换为字符串,然后将其转换为项目列表。

s = 'Apple,Mango,Banana'
print(f'List of Items in CSV ={s.split(",")}')

Output: List of Items in CSV =['Apple', 'Mango', 'Banana']

输出: List of Items in CSV =['Apple', 'Mango', 'Banana']

Python字符串到字符列表 (Python String to List of Characters)

Python String is a sequence of characters. We can convert it to the list of characters using list() built-in function. When converting a string to list of characters, whitespaces are also treated as characters. Also, if there are leading and trailing whitespaces, they are part of the list elements too.

Python字符串是字符序列。 我们可以使用内置的list()函数将其转换为字符列表 。 将字符串转换为字符列表时,空格也被视为字符。 另外,如果存在前导和尾随空格,它们也属于列表元素。

s = 'abc$ # 321 '

print(f'List of Characters ={list(s)}')

Output: List of Characters =['a', 'b', 'c', '$', ' ', '#', ' ', '3', '2', '1', ' ']

输出: List of Characters =['a', 'b', 'c', '$', ' ', '#', ' ', '3', '2', '1', ' ']

If you don’t want the leading and trailing whitespaces to be part of the list, you can use strip() function before converting to the list.

如果您不希望前导和尾随空格成为列表的一部分,则可以在转换为列表之前使用strip()函数

s = ' abc '

print(f'List of Characters ={list(s.strip())}')

Output: List of Characters =['a', 'b', 'c']

输出: List of Characters =['a', 'b', 'c']

That’s all for converting a string to list in Python programming.

这就是在Python编程中将字符串转换为列表的全部过程。

GitHub Repository.
GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/23750/python-convert-string-to-list

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/135127.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python实现二叉树层序遍历(逐层打印二叉树)「建议收藏」

    python实现二叉树层序遍历(逐层打印二叉树)「建议收藏」题目要求给定一个二叉树,要求从上往下逐层打印该二叉树节点的值,每层从左往右打印。解题思路——广度优先遍历实际上就是广度优先遍历,借助一个队列(这里用数组代替)就可以实现:1、先将root节点加入队列2、队列不为空时取队列首节点3、打印节点的值,然后将该节点的左、右子节点先后加入队尾(核心步骤,广度优先体现在这)4、回到2,直到队列为空该方法对满二叉树和非满二叉树都符合题目要求。…

    2022年5月21日
    46
  • idea激活码永久免费【2021免费激活】

    (idea激活码永久免费)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlMLZPB5EL5Q-eyJsa…

    2022年3月21日
    103
  • Windows编程(网络编程)

    Windows编程(网络编程)套接字类型与协议设置SOCK_STREAM[流套接字]TCP面向连接、可靠的数据传输适合传输大量的数据,不支持广播、多播SOCK_DGRAM[数据包套接字]

    2021年12月13日
    39
  • LM算法代码_快速排序算法代码

    LM算法代码_快速排序算法代码LM算法+推导+C++代码实践一、算法推导二、代码实践参考一、算法推导二、代码实践#include<Eigen/Dense>#include<Eigen/Sparse>#include<iostream>#include<iomanip>#include<math.h>usingnamespacestd;usingnamespaceEigen;constdoubleDERIV_STEP=1

    2022年9月27日
    2
  • linux(11)配置环境变量「建议收藏」

    linux(11)配置环境变量「建议收藏」前言在自定义安装软件的时候,经常需要配置环境变量,下面进行详细解析&nbsp;环境变量配置文件|用户|配置文件||:|:||系统环境|/ect/profil

    2022年7月28日
    9
  • JDK1.8关于运行时常量池, 字符串常量池的要点[通俗易懂]

    JDK1.8关于运行时常量池, 字符串常量池的要点[通俗易懂]网上关于jdk1.8的各种实验,结论鱼龙混杂,很多都相矛盾,网上有的实验也被后人测试出了不同的结果很多都分辨不了真假,这里记录一下网络上正确的结论,欢迎指正!首先自行区分运行时常量池与Class文件常量池(静态常量池)的概念,JVM内存模型,方法区与永久代的区别,有些在我的其他博客有介绍,连接在文尾在JDK1.7之前运行时常量池逻辑包含字符串常量池存放在…

    2022年7月28日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号