如何在Python中将列表转换为字符串?

如何在Python中将列表转换为字符串?Pythonprovidesdifferentvariabletypeforprogrammersusage.Wecanuseint,float,string,list,set…datatypesinourapplications.Whileusingdifferenttypeofvariableswemayneedtoconvert…

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

Python provides different variable type for programmers usage. We can use int, float, string, list, set … data types in our applications. While using different type of variables we may need to convert then to different types. In this tutorial we will different type of conversion from list to string in Python.

Python为程序员提供了不同的变量类型。 我们可以在应用程序中使用int,float,string,list,set…数据类型。 当使用不同类型的变量时,我们可能需要将其转换为不同类型。 在本教程中,我们将使用Python从列表到字符串的不同类型的转换。

使用联接转换 (Convert Using Join)

One of the most basic usage and implementation to convert list into string is converting list of strings with join function. Keep in mind that only list that only contains strings can be used with this method. As we can see that  each element is delimited with a single space in the new string.

将列表转换为字符串的最基本用法和实现之一是使用join函数将字符串列表转换。 请记住,此方法只能使用仅包含字符串的列表。 如我们所见,每个元素在新字符串中都用单个空格分隔。

mylist=['a','b','c'] 
' '.join(mylist) 
#'a b c'

转换不同的类型,例如整数 (Convert Different Types Like Integer)

As stated before we can convert a list which is only consist of string elements. But what if we need to convert a list which contains different type of data. We need some conversion into string. We will use str function to convert different data types into string.

如前所述,我们可以转换仅包含字符串元素的列表。 但是,如果我们需要转换包含不同类型数据的列表,该怎么办? 我们需要一些转换为字符串。 我们将使用str函数将不同的数据类型转换为字符串。

mylist = [1, 2, 3]      
' '.join(str(e) for e in mylist)        
#'1 2 3'

指定不同的分隔符 (Specify Different Delimiters)

Up to now we have provided space as separator in elements in new string. But we can specify different delimiters by changing space with new delimiter like , command.

到目前为止,我们已经在新字符串的元素中提供了space作为分隔符。 但是,我们可以通过改变指定不同的分隔符space与像新的分隔符,命令。

mylist=['a','b','c']                                                                                             
','.join(mylist) 
#'a,b,c'

指定要转换的范围 (Specify Range To Convert)

In some situations we may do not need to convert the whole list into string. In this situations we can specify the range we need to convert. In this example we will only convert first two element in a list. We will define the first two elements with [0:2]

在某些情况下,我们可能不需要将整个列表转换为字符串。 在这种情况下,我们可以指定需要转换的范围。 在此示例中,我们将仅转换列表中的前两个元素。 我们将使用[0:2]定义前两个元素

mylist=['a','b','c']                                                                                             
' '.join(str(e) for e in mylist[0:2])                                                                            
#'a b'

LEARN MORE  C Constants and Define Preprocessor

了解更多C常量并定义预处理器

翻译自: https://www.poftut.com/convert-list-string-python/

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

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

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


相关推荐

  • lwip udp 发送_lwip udp发送

    lwip udp 发送_lwip udp发送一、udp.c实现的函数1、void udp_input(structpbuf*p,structnetif*inp)说明:处理接收到的udp数据包。参数:p数据包缓存区;inp网络接口。   2、err_t udp_send(structudp_pc…

    2022年9月8日
    0
  • 嵌入在网站上Flash播放机(2)

    嵌入在网站上Flash播放机(2)

    2022年1月5日
    49
  • 不止一个背包的背包问题_算法基础课acwing下载

    不止一个背包的背包问题_算法基础课acwing下载有 N 种物品和一个容量是 V 的背包。物品一共有三类:第一类物品只能用1次(01背包);第二类物品可以用无限次(完全背包);第三类物品最多只能用 si 次(多重背包);每种体积是 vi,价值是 wi。求解将哪些物品装入背包,可使物品体积总和不超过背包容量,且价值总和最大。输出最大价值。输入格式第一行两个整数,N,V,用空格隔开,分别表示物品种数和背包容积。接下来有 N 行,每行三个整数 vi,wi,si,用空格隔开,分别表示第 i 种物品的体积、价值和数量。si=−1 表示第 i 种

    2022年8月9日
    3
  • IDEA 安装步骤「建议收藏」

    IDEA 安装步骤「建议收藏」1、下载与安装下载地址:https://www.jetbrains.com下载完成后安装选择安装的位置安装完成激活码:K03CHKJCFT-eyJsaWNlbnNlSWQiOiJLMDNDSEtKQ0ZUIiwibGljZW5zZWVOYW1lIjoibnNzIDEwMDEiLCJhc3NpZ25lZU5hbWUiOiIiLCJhc3NpZ25lZUVtYWlsIjoi…

    2022年9月1日
    3
  • clion激活_最新在线免费激活

    (clion激活)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月29日
    74
  • CPU型号后缀字母所代表的含义

    CPU型号后缀字母所代表的含义一、Intel桌面式CPU——只看数字你就输了●X后缀X后缀=至高无上的至尊版  X代表Extreme,中文意思是至尊级,代表同一时代性能最强的CPU。如Corei7-5960X、Corei7-4960X。X代表在同一代中只有一款CPU黄袍加身,地位至高无上。加上没有竞争对手可以望其项背,…

    2022年5月30日
    38

发表回复

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

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