TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Python Join() Method
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python BasicPython Tutorials

Python Join() Method

Last updated: Sep 25, 2023 12:27 am
By Meenakshi Agarwal
Share
5 Min Read
Python join() method with examples
Python join() method with examples
SHARE

In this tutorial, we will discuss the Python join() method and learn how to use it. Join() can be used to combine strings, lists, and dictionaries. We will provide several examples to help you understand its usage.

The primary use case of this method is to join a sequence of types like strings, tuples, numbers, and dictionaries and convert them into a String. It is quite a simple method to use which concatenates elements of a collection with a separator. Let’s now take a sequence of each data type one by one and tries to join with examples.

Python Join() Description & Syntax

The join() method allows an iterable as the input parameter and concatenates its elements into a string with some separator. We can pass any string separator such as a comma, space, or an underscore, etc. Interestingly, you can even specify an actual string value such as “XYZ” or “ABC,” etc.

It has the following syntax which you need to remember for use in Python programs.

# Python Join Syntax

string_sep.join( python_seq )

Here, string_sep is the separator or delimiter that you want to use to join the elements, and python_seq is the collection of elements that you want to join.

The join() method is particularly useful when you have a list of strings and you want to combine them into a single string.

Join Strings in a List

We can pass a list of string type values or chars to join() method. It will concatenate all the words in a list or each letter in the case of characters. The returned value is a unified string object connected with a given separator.

Languages = ['Python', 'CSharp', 'JavaScript', 'AngularJS']
token = ', '
print(token.join(Languages))

Chars = ['a', 'g', 'i', 'l', 'e', ' ', 's', 'c', 'r', 'u', 'm']
token = ''
print(token.join(Chars))

The first sample in the above example will produce a comma-separated string. The second one would just concatenate all the characters into one string. See the results below.

Python, CSharp, JavaScript, AngularJS
agile scrum

Join Numbers in a List

The input sequence could hold a group of integers. In this case, we first need to convert each of them to a string and then join them.

Primes = ['2', '3', '5', '7', '11', '13']
token = '->'
print(token.join(str(iter) for iter in Primes))

mixedList = ['I', 'work', 24, 'hrs', 'a', 'day', 'and', 365, 'days', 'a', 'year.']
token = ' '
print(token.join(str(iter) for iter in mixedList))

Check the output below:

2->3->5->7->11->13
I work 24 hrs a day and 365 days a year.

Concatenate Elements of a Tuple

The tuple is also a type of sequence that we can pass to join(). It then combines all its elements into one.

inputTuple = ("Test1", "Test2", "Test3", "Test4")
sep = '_'
out = sep.join(inputTuple)
print(out)

Output:

Test1_Test2_Test3_Test4

Let’s check out another tuple example. In this, the tuple has mixed type values. Hence, we’ll first need to convert each of them to a string using the map() function.

inputTuple = ("Test", 1, "Test", 2, "Test", 3, "Test", 4)
sep = '_'
out = sep.join(map(str, inputTuple))
print(out)

Output:

Test_1_Test_2_Test_3_Test_4

Combine Keys of a Dictionary

Python join() method can also accept a dictionary and combines all its key fields as one. See the below example.

inputDict = {"Counry": "USA", "City": "NewYork", "Street": "Wall Street"}
separator = "_"

out = separator.join(inputDict)

print("After join: " + out)
print("Join() return type: {}".format(type(out)))

The output:

After join: Counry_City_Street
Join() return type: <class 'str'>

Join Elements in a Set

Let’s see now how Join() works when a set type sequence is passed as its argument.

GangOfFour = {'Gamma', 'Helm', 'Johnson', 'Vlissides'}
token = '_'
print(token.join(GangOfFour))

SetOfPrimes = {'2', '3', '5', '7', '11', '13'}
token = ', '
print(token.join(SetOfPrimes))

The result is as follows:

Vlissides_Gamma_Helm_Johnson
13, 3, 2, 7, 11, 5

Must Read – Convert List to String

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Convert Python list to string with examples Convert a List to String in Python
Next Article How to Handle iFrame in Selenium Handle iFrame/iFrames in Selenium Webdriver

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x