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

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.