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月10日
    39
  • VS2015序列号_Idm序列号无效了

    VS2015序列号_Idm序列号无效了1、VS2008简体中文正式版序列号1.VisualStudio2008ProfessionalEdition:XMQ2Y-4T3V6-XJ48Y-D3K2V-6C4WT2.VisualStudio2008TeamTestLoadAgent:WPX3J-BXC3W-BPYWP-PJ8CM-F7M8T3.VisualStudio2008TeamSyst…

    2022年8月10日
    11
  • kvm虚拟机xml文件在哪里_爱快kvm虚拟机

    kvm虚拟机xml文件在哪里_爱快kvm虚拟机virshxml创建kvm虚拟机准备工作sudoapt-getupdatesudoapt-getinstallqemu-kvmsudoapt-getinstallvnc4serversudoapt-getinstallbridge-utils增加网卡内容如下:root@zhangji16vm:/home/prj1#cat/etc/network/in…

    2022年8月11日
    6
  • iOS中的屏幕适配

    iOS中的屏幕适配

    2021年9月14日
    48
  • html中下拉菜单(html做下拉菜单栏)

    html5如何实现文本框下拉选项功能使用html5或者jQuery如何实现文本框下拉效果,如下图CSS布局HTML小编今天和大家分享大神详解最好有可以使用HTML5list属性。list属性需要应用到input框上,然后内容写一个自定义的id然后在任意位置放一对datalist标签,并给datalist框一个id,和list属性指向的id一致即可。在datal…

    2022年4月18日
    44
  • STM32看门狗总结

    STM32看门狗总结转自:http://www.openedv.com/thread-56260-1-1.htmlSTM32看门狗总结调原子哥的开发板一年多,基本上能用,但是对于STM32某些基本外设的工作机理还不甚明了。借此暑假的机会对各个外设的功能做一个简短的总结,在提高自己基础知识的同时,也给其他同学提供一些参考。先来看门狗部分的内容。看门狗部分内容当中较难理解的是窗口看门狗

    2022年6月13日
    24

发表回复

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

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