C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python String join() MethodPython join() method is used to concat a string with iterable object. It returns a new string which is the concatenation of the strings in iterable. It throws an exception TypeError if iterable contains any non-string value. It allows various iterables like: List, Tuple, String etc. Signaturejoin(iterable) Parametersiterable : iterable object like: List, Tuple, String etc. ReturnIt returns a new string or an exception TypeError if iterable contains any non-string value. Let's see some examples of join() method to understand it's functionalities. Python String join() Method Example 1A simple example which implements join() method with the List iterable, see the example below. # Python join() method example # Variable declaration str = ":" # string list = ['1','2','3'] # iterable # Calling function str2 = str.join(list) # Displaying result print(str2) Output: 1:2:3 Python String join() Method Example 2A list iterable join with empty string and produce a new string, see the example below # Python join() method example # Variable declaration str = "" # string list = ['J','a','v','a','t','p','o','i','n','t'] # iterable # Calling function str2 = str.join(list) # Displaying result print(str2) Output: TheDeveloperBlog Python String join() Method Example 3An example of join() method with Set iterable. Set contains unordered elements and produce different output each times. See the example below. # Python join() method example # Variable declaration str = "->" # string list = {'Java','C#','Python'} # iterable # Calling function str2 = str.join(list) # Displaying result print(str2) Output: Java->Python->C# Python String join() Method Example 4In case of dictionary, this method join keys only. Make sure keys are string, otherwise it throws an exception. # Python join() method example # Variable declaration dic = {'key1': 1, 'key2': 2} str = '&' # Calling function str = str.join(dic) # Displaying result print(str) Output: key1&key2
Next TopicPython Strings
|