I don't want to use a for statement, but I want to combine characters in the list into a single string
a = ['a','b','c','d']
Same list type
a = 'abcd'
I'm going to change it to one string My code was made using a for statement, but how can I make it shorter without using a for statement?
a = ['a','b','c','d']
result = ""
for i in a:
result += i
Use the str.join(iterable) method in an empty string.
The str.join(iterable) method attaches itable after the str.
a = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(''.join(a))
print(''.join(a[:4]))
Output:
abcdefg
abcd
© 2024 OneMinuteCode. All rights reserved.