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)
上一篇 2022年6月5日 下午7:00
下一篇 2022年6月5日 下午7:00


相关推荐

  • 《数据分析实战:基于EXCEL和SPSS系列工具的实践》——1.5 如何成为数据分析高手…

    《数据分析实战:基于EXCEL和SPSS系列工具的实践》——1.5 如何成为数据分析高手…

    2022年3月4日
    44
  • 页面可见性改变事件:visibilitychange

    页面可见性改变事件:visibilitychange1、PC浏览器上(以谷歌浏览器为例)刷新H5页面,会触发该事件,由于刷新导致该页面消失时,会检测到document.visibilityState===‘hidden’切换页面(包括切换离开和切换回来),导致页面暂时不处于激活状态时,会触发该事件。切换离开时document.visibilityState===‘hidden’,切换回该页面时,document.visibilityS…

    2022年6月18日
    45
  • Nginx原理简述

    Nginx原理简述Nginx 原理简介 1 nginx 的作用反向代理 隐藏服务器的地址 负载均衡 轮询 权重 ip hash 动静分离 nginx 作为静态资源服务器 对 nginx 的主要操作都是在改配置文件 2 nginx 原理 nginx 是以进程为单位的 这里是进程不是线程 每个进程有自己独立的资源 不用再像线程一样加锁了 如果你刚下载好了 nginx 你启动之后 打开任务管理器看一下有几个 nginx 进程 如果不出意外 它会有两个 nginx 进程 为什么会有两个进程呢 原理解析 nginx 里面 有两

    2025年8月11日
    5
  • Canny算子中的非极大值抑制(Non-Maximum Suppression)分析

    Canny算子中的非极大值抑制(Non-Maximum Suppression)分析非极大值抑制详解 Non MaximumSuppr blog csdn net kezunhai nbsp nbsp nbsp nbsp nbsp 在前面介绍图像不变特征算子的时候 很多处都用到了非极大值抑制 对于非极大值抑制的分析 很多论文或资料都是一带而过 留下傻傻的自己似懂非懂 就那么忽悠过了 本文针对日常中应用的非极大值抑制进行详细的分析

    2026年3月19日
    2
  • 配置管理项的定义

    配置管理项的定义项目立项后 无论项目规模如何 基于管理需要 我们都会建立配置管理计划 配合项目的有效管理 根据项目特点及管理模式 项目配置项的建立各不相同 可是 无论怎么改变 最终的目标都是一个 方便项目管理 便于资源共享 nbsp nbsp nbsp 最近一个项目中 我在配置项中采取如下思想进行配置计划 nbsp nbsp nbsp 一级目录 以 trunk branches tags 为起点 考虑到配置管理工具 我们选择流行的 svn 基线 trun

    2025年7月2日
    2
  • OpenClaw 不回复排障:一次“配置都对但还是超时”的真实复盘

    OpenClaw 不回复排障:一次“配置都对但还是超时”的真实复盘

    2026年3月12日
    4

发表回复

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

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