Concatenate multiple strings: +, += operator,Concatenate a list of strings into one string: join(),Split strings in Python (delimiter, line break, regex, etc.),This article describes how to concatenate strings in Python.
s = 'aaa' + 'bbb' + 'ccc' print(s) # aaabbbccc s1 = 'aaa' s2 = 'bbb' s3 = 'ccc' s = s1 + s2 + s3 print(s) # aaabbbccc s = s1 + s2 + s3 + 'ddd' print(s) # aaabbbcccddd
s1 += s2 print(s1) # aaabbb
s = 'aaa' s += 'xxx' print(s) # aaaxxx
s = 'aaa' 'bbb' 'ccc' print(s) # aaabbbccc
s = 'aaa' 'bbb' 'ccc' print(s) # aaabbbccc s = 'aaa'\ 'bbb'\ 'ccc' print(s) # aaabbbccc
# s = s1 s2 s3 # SyntaxError: invalid syntax
The join() string method returns a string by joining all the elements of an iterable (list, string, tuple), separated by a string separator.,The join() method returns a string created by joining the elements of an iterable by string separator.,Note: The join() method provides a flexible way to create strings from iterable objects. It joins each element of an iterable (such as list, string, and tuple) by a string separator (the string on which the join() method is called) and returns the concatenated string.,The join() method takes an iterable (objects capable of returning its members one at a time) as its parameter.
Example
text = ['Python', 'is', 'a', 'fun', 'programming', 'language'] # join elements of text with space print(' '.join(text)) # Output: Python is a fun programming language
The syntax of the join()
method is:
string.join(iterable)
Example 1: Working of the join() method
#.join() with lists numList = ['1', '2', '3', '4'] separator = ', ' print(separator.join(numList)) #.join() with tuples numTuple = ('1', '2', '3', '4') print(separator.join(numTuple)) s1 = 'abc' s2 = '123' # each element of s2 is separated by s1 # '1' + 'abc' + '2' + 'abc' + '3' print('s1.join(s2):', s1.join(s2)) # each element of s1 is separated by s2 # 'a' + '123' + 'b' + '123' + 'b' print('s2.join(s1):', s2.join(s1))
Example 2: The join() method with sets
#.join() with sets test = { '2', '1', '3' } s = ', ' print(s.join(test)) test = { 'Python', 'Java', 'Ruby' } s = '->->' print(s.join(test))
Example 3: The join() method with dictionaries
#.join() with dictionaries test = { 'mat': 1, 'that': 2 } s = '->' # joins the keys only print(s.join(test)) test = { 1: 'mat', 2: 'that' } s = ', ' # this gives error since key isn 't string print(s.join(test))
Use str.join
:
>>> words = ['this', 'is', 'a', 'sentence'] >>>
'-'.join(words)
'this-is-a-sentence' >>>
' '.join(words)
'this is a sentence'
A more generic way to convert python lists to strings would be:
>>> xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
''.join(map(str, xs))
'12345678910'
Once you learn it, it's very comfortable and you can do tricks like this to add parentheses.
>>> ",".join("12345").join(("(", ")"))
Out:
'(1,2,3,4,5)'
>>>
list = ["(", ")"] >>>
",".join("12345").join(list)
Out:
'(1,2,3,4,5)'
Although @Burhan Khalid's answer is good, I think it's more understandable like this:
from str
import join
sentence = ['this', 'is', 'a', 'sentence']
join(sentence, "-")
list_abc = ['aaa', 'bbb', 'ccc']
string = ''.join(list_abc)
print(string) >>>
aaabbbccc
string = ','.join(list_abc)
print(string) >>>
aaa, bbb, ccc
string = '-'.join(list_abc)
print(string) >>>
aaa - bbb - ccc
string = '\n'.join(list_abc)
print(string) >>>
aaa >>>
bbb >>>
ccc
We can also use Python's reduce function:
from functools
import reduce
sentence = ['this', 'is', 'a', 'sentence']
out_str = str(reduce(lambda x, y: x + "-" + y, sentence))
print(out_str)
To recap, Python string implements the join() method. You can pass it an iterable argument. The join method loops through the elements of the iterable and combines them into a single string.,Call the join method on the separator string by passing the dictionary values as an argument.,When it comes to merging strings, you can pass any of these iterable types as an argument in the join() method call.,The join() method treats a string as a separator value for merging strings from an iterable.
words = ["This", "is", "a", "test"]
joined = "-".join(words)
print(joined)
Output:
This - is - a - test
Here is how it looks in code.
words = ["This", "is", "a", "test"]
joined = " ".join(words)
print(joined)
For instance, given a list of number strings, let’s join them and separate them by letter “x”:
nums = ["5", "3", "10"]
joined = "x".join(nums)
print(joined)
For example:
nums = ["5", "3", "10"]
joined = "x".join(nums)
print(joined)
Join(String, Object[]) is a convenience method that lets you concatenate each element in an object array without explicitly converting its elements to strings. The string representation of each object in the array is derived by calling that object's ToString method.,Join<T>(String, IEnumerable<T>) is a convenience method that lets you concatenate each member of an IEnumerable<T> collection without first converting them to strings. The string representation of each object in the IEnumerable<T> collection is derived by calling that object's ToString method.,Concatenates an array of strings, using the specified separator between each member, starting with the element in value located at the startIndex position, and concatenating up to count elements.,Concatenates an array of strings, using the specified separator between each member, starting with the element located at the specified index and including a specified number of elements.
public:
static System::String ^ Join(char separator, ... cli::array <System::Object ^> ^ values);
public:
static System::String ^ Join(char separator, ... cli::array <System::Object ^> ^ values);
public static string Join(char separator, params object ? [] values);
public static string Join (char separator, params object?[] values);
public static string Join(char separator, params object[] values);
static member Join : char * obj[] -> string
Public Shared Function Join(separator As Char, ParamArray values As Object()) As String
public:
static System::String ^ Join(char separator, ... cli::array <System::String ^> ^ value);
public:
static System::String ^ Join(char separator, ... cli::array <System::String ^> ^ value);
public static string Join(char separator, params string ? [] value);
public static string Join (char separator, params string?[] value);
public static string Join(char separator, params string[] value);
static member Join : char * string[] -> string
Public Shared Function Join(separator As Char, ParamArray value As String()) As String
public:
static System::String ^ Join(System::String ^ separator, System::Collections::Generic::IEnumerable<System::String ^> ^ values);
public:
static System::String ^ Join(System::String ^ separator, System::Collections::Generic::IEnumerable<System::String ^> ^ values);
public static string Join (string separator, System.Collections.Generic.IEnumerable<string> values);
public static string Join (string separator, System.Collections.Generic.IEnumerable<string> values);
public static string Join (string? separator, System.Collections.Generic.IEnumerable<string?> values);
[System.Runtime.InteropServices.ComVisible(false)]
public static string Join (string separator, System.Collections.Generic.IEnumerable<string> values);
static member Join : string * seq<string> -> string
static member Join : string * seq<string> -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
static member Join : string * seq<string> -> string
public:
static System::String ^ Join(System::String ^ separator, ... cli::array <System::Object ^> ^ values);
public:
static System::String ^ Join(System::String ^ separator, ... cli::array <System::Object ^> ^ values);
public static string Join(string separator, params object[] values);
public static string Join (string separator, params object[] values);
public static string Join(string ? separator, params object ? [] values);
[System.Runtime.InteropServices.ComVisible(false)]
public static string Join (string separator, params object[] values);
static member Join: string * obj[] - > string
static member Join : string * obj[] -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
static member Join : string * obj[] -> string
public:
static System::String ^ Join(System::String ^ separator, ... cli::array <System::String ^> ^ value);
public:
static System::String ^ Join(System::String ^ separator, ... cli::array <System::String ^> ^ value);
public:
static System::String ^ Join(System::String ^ separator, cli::array <System::String ^> ^ value);
public:
static System::String ^ Join(System::String ^ separator, cli::array <System::String ^> ^ value);
public static string Join(string separator, params string[] value);
public static string Join (string? separator, params string?[] value);
public static string Join(string separator, string[] value);
public static string Join (string separator, string[] value);
static member Join: string * string[] - > string
public:
static System::String ^ Join(char separator, cli::array <System::String ^> ^ value, int startIndex, int count);
public:
static System::String ^ Join(char separator, cli::array <System::String ^> ^ value, int startIndex, int count);
public static string Join(char separator, string ? [] value, int startIndex, int count);
public static string Join (char separator, string?[] value, int startIndex, int count);
public static string Join(char separator, string[] value, int startIndex, int count);
static member Join : char * string[] * int * int -> string
Public Shared Function Join(separator As Char, value As String(), startIndex As Integer, count As Integer) As String