+ Operator के बिना String Concatenation
आप String Class से str.join(list_of_strings) का उपयोग कर सकते हैं। जिस String पर आप इसे Call करते हैं, उसका उपयोग आपके द्वारा Argument के रूप में Provide की गई Strings की List को Join करने के लिए किया जाता है।
उदाहरण
>>> ' '.join(['Hello', 'World']) 'Hello World' >>> ''.join(['Hello', 'World', 'People']) 'HelloWorldPeople' >>> ', '.join(['Hello', 'World', 'People']) 'Hello, World, People'
ध्यान दें कि जिस String पर हम इसे Call करते हैं, उसे हमारे द्वारा Argument के रूप में Pass की गई Strings की List के बीच Insert किया जाता है।
Other Methods:
1. String Formatting:
>>> a, b, c = 'Hello', 'World', 'People'
>>> f"{a} {b} {c}"
'Hello World People'
>&>> "{} {} {}".format(a, b, c)
'Hello World People'
>>> "%s %s %s" % (a, b, c)
'Hello World People'
2. io.StringIO (Large Concatenations के लिए):
import io
buffer = io.StringIO()
for i in range(10000):
buffer.write(str(i))
result = buffer.getvalue()
3. List Comprehension + join:
parts = ['Hello', 'World', 'People'] result = ''.join(parts) # या ' '.join(parts)
4. print() Function (Output के लिए):
>>> print('Hello', 'World', 'People', sep=' ')
Hello World People
>>> print('Hello', 'World', 'People', sep=', ')
Hello, World, People
Performance Summary:
| Method | Best For |
|---|---|
join() |
List of Strings, Known Separator |
f-strings |
Few Variables, Formatting Needed |
StringIO |
Large/Unknown Number of Concats |
print(sep=...) |
Direct Output |