如何在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/

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 腾讯ssl 免费证书_手机云证书

    腾讯ssl 免费证书_手机云证书SSL证书和我们日常用的身份证类似,是一个支持HTTPS网站的身份证明,SSL证书里面包含了网站的域名,证书有效期,证书的颁发机构以及用于加密传输密码的公钥等信息,由于公钥加密的密码只能被在申请证书时生成的私钥解密,因此浏览器在生成密码之前需要先核对当前访问的域名与证书上绑定的域名是否一致,同时还要对证书的颁发机构进行验证,如果验证失败浏览器会给出证书错误的提示。SSL证书对于网站数据的安全传输起…

  • 齿轮常见故障与特征频率及其谐波、边频带

    齿轮常见故障与特征频率及其谐波、边频带

    2021年11月22日
  • 深入理解linux内存管理_linux内存是如何划分的

    深入理解linux内存管理_linux内存是如何划分的摘要:本章首先以应用程序开发者的角度审视Linux的进程内存管理,在此基础上逐步深入到内核中讨论系统物理内存管理和内核内存的使用方法。力求从外到内、水到渠成地引导网友分析Linux的内存管理与使用。在本章最后,我们给出一个内存映射的实例,帮助网友们理解内核内存管理与用户内存管理之间的关系,希望大家最终能驾驭Linux内存管理。前言内存管理一向是所有操作系统书籍不惜笔墨重点讨论的内容,无论市

    2022年10月31日
  • 实验报告:图书销售管理系统数据库SQL应用编程

    实验报告:图书销售管理系统数据库SQL应用编程实验目的针对图书销售管理数据库开发,了解SQL语言DDL、DML、DQL类型语句在数据库操作访问中的应用方法,培养数据库SQL编程访问能力。同时也掌握基本的数据库触发器、存储过程SQL编程方法,培养数据库后端编程能力。本实验完成图书销售管理系统数据库的SQL数据操作访问和后端数据处理功能。实验原理首先对图书销售管理系统进行数据需求分析,定义组成系统数据结构的实体、实体属性以及实体之间的关系。采用实体关系图(E-R模型图)方法来展示图书销售管理系统的概念数据模型与逻辑数据模型。利用PowerDes

  • java -jar启动时设定springboot日志输出级别

    java -jar启动时设定springboot日志输出级别

  • css3 媒体类型(Media Type)[通俗易懂]

    css3 媒体类型(Media Type)[通俗易懂]linkhref=”css/reset.css”rel=”stylesheet”type=”text/css”media=”screen”/>linkhref=”css/style.css”rel=”stylesheet”type=”text/css”media=”all”/>linkhref=”css/print.css”rel=”stylesheet”ty

发表回复

您的电子邮箱地址不会被公开。

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