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修改ip地址_怎么更改电脑ip地址?基于 Python 爬虫的ip修改设计与实现

    python修改ip地址_怎么更改电脑ip地址?基于 Python 爬虫的ip修改设计与实现怎么更改电脑ip地址?基于Python爬虫原理的篮球鞋选择程序的设计与实现ip修改【摘要】伴随着篮球鞋工艺的进步及产业升级,多类型多种类的篮球鞋出现在大众的视野当中。与此同时,消费者对篮球鞋的选择也逐渐增多。针对篮球爱好者在篮球鞋认知存在选择局限性、认知局限性等问题,针对于市面上关于篮球鞋选择程序的空白,也为了可以让球鞋爱好者选择合适的球鞋,本文笔者尝试通过利用Python爬虫,定向抓取…

    2022年6月20日
    29
  • python的源代码下载_官方下载python源码,编译linux版本的python「建议收藏」

    python的源代码下载_官方下载python源码,编译linux版本的python「建议收藏」我这里使用的时centos7-mini,centos系统本身默认安装有python2.x,版本x根据不同版本系统有所不同,可通过python–V或python–version查看系统自带的python版本有一些系统命令时需要用到python2,不能卸载1、安装依赖包1)首先安装gcc编译器,gcc有些系统版本已经默认安装,通过gcc–version查看,没安装的先安装g…

    2022年8月23日
    11
  • 重复字符串 leetcode_无重复字符的最长子串c语言

    重复字符串 leetcode_无重复字符的最长子串c语言原题链接给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。示例 1:输入: s = “abcabcbb”输出: 3 解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。示例 2:输入: s = “bbbbb”输出: 1解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。示例 3:输入: s = “pwwkew”输出: 3解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。 请注意,你的答案必须是 子串 的长度,”pwk

    2022年8月9日
    4
  • spring在LInux下出现的问题【转】

    spring在LInux下出现的问题【转】

    2022年3月2日
    33
  • MyBatis标签详解

    MyBatis标签详解MyBatis标签详解

    2022年4月22日
    64
  • Java 里的异常(Exception)详解

    Java 里的异常(Exception)详解作为一位初学者,本屌也没有能力对异常谈得很深入.只不过Java里关于Exception的东西实在是很多.所以这篇文章很长就是了..一,什么是java里的异常由于java是c\c++发展而

    2022年7月4日
    26

发表回复

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

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