[英]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]
Use f-strings or formatted string literals and list comprehension :使用f-strings 或格式化的字符串文字和列表理解:
lst = [f' {x} ' for x in lst]
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.