TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python String | join() method with Examples

Python String join() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.

<< Back to PYTHON

Python String join() Method

Python 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.

Signature

join(iterable)

Parameters

iterable : iterable object like: List, Tuple, String etc.

Return

It 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 1

A 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 2

A 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 3

An 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 4

In 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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf