简体   繁体   English

如何在列表元素之前和之后放置一个空格?

[英]How I can put one space before and after list elements?

How I can put one space before and after list elements?如何在列表元素之前和之后放置一个空格?

For example list=['A','B','C']例如list=['A','B','C']

I want我想

list=[' A ',' B ',' C ']

#Simple way
list = [' '+x+' ' for x in list] 

#F-strings way
lits = [f' {x} ' for x in list]

Didnt you just answer the question yourself你不是自己回答问题吗

by using:通过使用:

list = [' A ',' B ',' C ']

The code already works when you print打印时代码已经有效

print(list[0])

it will print 'A' with a space before and after it它将在其前后打印带有空格的“A”

In order to verify this see the difference by printing the first charectors of the first list and second list为了验证这一点,通过打印第一个列表和第二个列表的第一个字符来查看差异

first list = list = ['A', 'B', 'C'] second list = list = [' A ', ' B ', ' C ']第一个列表=列表=['A','B','C']第二个列表=列表=['A','B','C']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何 select 列表的元素从列表中的后一个元素开始到 python 之前的元素 - How can I select elements of a list starting by a later element in the list to one that is before in python 如何在列表列表中按空格和引号拆分元素? - How can I split elements by space and quotation within a list of list? 如何将列表的单个元素放入嵌套列表中? - How can I put individual elements of a list into nested list? 从列表中排除特定的第 n 个元素后,如何从列表中获取“最后一个”? - How can I get the "last one" from the list after excluding a specific nth elements from the list? 如何将来自 for 循环的各种元素放入列表中? - How can I put the various elements come from for loop in a list? Python中如何在字符前后插入空格并换行? - How to insert a space before and after a character and put on a new line in Python? 如何将一个值放入另一个列表? - How can I put one value in another list? 如何在列表中一一创建元素的组合? - How can I create combinations of elements in a list one by one? 使python configobj不在'='之前和之后放置空格 - Make python configobj to not put a space before and after the '=' 如何检查数字是否可以被列表中的元素之一整除(Python)? - How can I check if a number is divisible by one of the elements in a list (Python)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM