前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >如何在Python中将列表转换为字符串?

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

作者头像
全栈程序员站长
发布2022-08-12 17:51:55
发布2022-08-12 17:51:55
4.5K00
代码可运行
举报
运行总次数:0
代码可运行

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

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函数将字符串列表转换。 请记住,此方法只能使用仅包含字符串的列表。 如我们所见,每个元素在新字符串中都用单个空格分隔。

代码语言:javascript
代码运行次数:0
运行
复制
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函数将不同的数据类型转换为字符串。

代码语言:javascript
代码运行次数:0
运行
复制
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与像新的分隔符,命令。

代码语言:javascript
代码运行次数:0
运行
复制
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]定义前两个元素

代码语言:javascript
代码运行次数:0
运行
复制
mylist=['a','b','c']                                                                                             
' '.join(str(e) for e in mylist[0:2])                                                                            
#'a b'

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月3,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用联接转换 (Convert Using Join)
  • 转换不同的类型,例如整数 (Convert Different Types Like Integer)
  • 指定不同的分隔符 (Specify Different Delimiters)
  • 指定要转换的范围 (Specify Range To Convert)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档