How I can put one space before and after list elements?
For example 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 :
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
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 ']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.