Home » Python Tutorial » Python Split String with Examples

Python Split String with Examples

Python string split implies splitting or dividing the given string into tiny parts, as the name itself suggests.

If anyone has worked in some programming language on strings, then you could know about concatenation (mixing strings) and it’s just the inverse of string break. This coding language supports us with a built-in function called split, to implement the split function in python on strings ().

You will learn how to divide a string in Python that means python split string in this article, with some well written and displayed Python sample programs.

What is the exact syntax of python split?

The basic signature of python split is displayed below:

str.split([separator_prog=None] , [maxsplit=-1])

What are the parameters or arguments and the retrieval type of python split?

The python split has 2 types of parameters or arguments. One is a separator which means that a given string in the code behaves as an argument or parameter. The other is max split which means how many times the split function has to be performed in the given code.

The return or retrieval type of python split is some list in which the items are divided by the comma.

Illustrations of python split methodology

Below mentioned is an illustration of how to split python is carried out practically in nature. Here, you go!

#Let’s declare some string that is to be divided or splitted
Text_demo= ‘Python is easiest and efficient to code’
#Let's apply the python split procedure to divide the above-declared string Text_demo
sp= Text_demo.split()
#Let’s print the result 
print(“The result after the split methodology is: ”, sp)

# Output of this illustration will be:

# The result after the split methodology is: [‘Python’,‘is’,‘easiest’,‘and’,‘efficient’,‘to’,‘code’]

Example 2

Another illustration that utilizes the python split procedure is shown below:

text_demo2= ‘Apple is worth eating’

#The result is splitted at the space
print(“The result of this 1st string is: ”, text.split())

demo2_st = ‘Py, th, on’

#The result is splitted at the symbol comma(,)
print(“The result of this 2nd string is: ”.demo2_st.split(', '))

#The result is splitted at the symbol colon ‘:’
print(“The result of this 3rd string is: ”,demo2_st.split(':'))

# Output of this illustration will be:

# The result of this 1st string is: [‘Apple’, ‘is’, ‘worth’, ‘eating’]
# The result of this 2nd string is: [‘Py’, ‘th’, ‘on’]
# The result of this 3rd string is: [‘Py, th, on’]

Example 3

The further illustration that displays how to utilize the methodology namely python split is as mentioned below:

coding_langs=‘python, java, ruby, sql’

# The result displayed will have the max split of the string=2
print(“The result of the declared string having max split of 2 is: ”, coding_langs.split(', ', 2))

# The result displayed will have the max split of the string=1
print(“The result of the declared string having max split of 1 is: ”, coding_langs.split(', ', 1))

# The result displayed will have the max split of the string=5
print(“The result of the declared string having max split of 5 is: ”, coding_langs.split(', ', 5))

# The result displayed will have the max split of the string=0
print(“The result of the declared string having max split of 0 is: ”, coding_langs.split(', ', 0))

# Output of this illustration will be:

# The result of the declared string having max split of 2 is: [‘python’, ‘java’, ‘ruby, sql’]

# The result of the declared string having max split of 1 is: [‘python’, ‘java, ruby, sql’]

# The result of the declared string having max split of 5 is: [‘python’, ‘java’, ‘ruby’, ‘sql’]

# The result of the declared string having max split of 0 is: [‘python, java, ruby, sql’]
Did you know?
1. Python Zip Function
2. Python Break Statement with Examples
3. Python Input Function and Print Function
4. Python Bisect Algorithm: Bisect and Insort Functions
5. Palindrome in Python
6. Armstrong Number In Python
7. Absolute Value in Python
8. Leap Year Program in Python
9. Factorial Program in Python
10. Python Split with Examples

Pin It on Pinterest