Home » Python

Python String | join() Method with Example

Python String| join() Method with Example, this is an in-built Method in Python, it is used to join the elements of the list, string with a separator.
Submitted by IncludeHelp, on July 18, 2018

join() is an in-built method in Python and it is used to join elements of the list, string etc with the given str separator.

Note: Method is called with the separator and as arguments elements are provided, then the final (returned) string is a joined string by the separator string.

Syntax:

 String.join(iterable)

Example 1: Joining string with hyphen ('-') character

# s as separator string 
s = "-"
# str as sequence of strings
str = ("Hello", "World", "How are?")

# join and print the string
print (s.join(str))

Output

    Hello-World-How are?

Example 2: Joining string with spaces, a student detail is provided as string sequences

# s as separator string (contains - space)
s = " "

# a student details
first_name = "Amit"
second_name = "Shukla"
age = "21"
branch = "B.Tech"

# creating string sequences
str = (first_name, second_name, age, branch)

# value of str before joining
print "str before join..."
print (str)


# join and print the string
print "str after join..."
print (s.join(str))

Output

    str before join...
    ('Amit', 'Shukla', '21', 'B.Tech')
    str after join...
    Amit Shukla 21 B.Tech


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.