Python Tutorial - Join Function

Python Exercise Lists

How to merge strings in Python?

The join () method is also a very important string method. It is the inverse of the split () method and is used to join multiple strings contained in a list (or tuple) into a single string.

Readers who want to know more about the split() method can read the "Python split () method" section.

When you use the join() method to merge strings, it joins multiple strings in a list (or tuple) together with a fixed separator. For example, the string "" can be seen as the result of merging the list of ['www', 'freelearningpoints', 'com'] into one string with the separator ".".

The syntax of the join () method is as follows:

new_string = str.join(iterable)

The meaning of each parameter in this method is as follows:

  • new_string: the new string generated after merging;
  • str: used to specify the separator when merging;
  • iterable: The source string data for the merge operation, which can be provided in the form of lists, tuples, etc.

[Example] Combine the strings in the list into one string.

a = ["www", "freelearningpoints", "com"]
type(a)  #check the class of a

The output is:


[Example] Combine the strings in the tuple into one string.

b = '','user','desktop','python'
type(b) #check the class of a

The output is:


Python - Count Function

How to count the number of times a string appears in Python?

The count method is used to retrieve the number of occurrences of the specified string in another string. If the retrieved string does not exist, it returns 0, otherwise it returns the number of occurrences.

The syntax of the count method is as follows:


The specific meaning of each parameter in this method is as follows:

  • str: the original string;
  • sub: the substring to be retrieved;
  • start: Specify the starting position of the search, that is, where to start the detection. If not specified, search from the beginning by default;
  • end: Specifies the end position of the search. If not specified, it means that the search is continued to the end.

[Example] Retrieve the number of occurrences of "e" in the string "".

string = ""
string = ""
string.count('e', 1)
string.count('e', 2)

The output is:


As mentioned earlier, the search value corresponding to each character in the string starts from 0, so the search value 1 in this example corresponds to the second character '.', Which can be analyzed from the output result, and the search starts from the specified index position. Which also contains this index position.


string = ""
string.count('e', 2, -3)
string.count('e', 2, -4)

The output is:



More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises