Python slice string to list



Description

Python string method split() returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num.

Syntax

Following is the syntax for split() method −

str.split(str="", num=string.count(str)).

Parameters

  • str − This is any delimeter, by default it is space.

  • num − this is number of lines minus one

Return Value

This method returns a list of lines.

Example

The following example shows the usage of split() method.

#!/usr/bin/python

str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
print str.split( )
print str.split(' ', 1 )

When we run above program, it produces following result −

['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']

python_strings.htm

What is Split in Python?

The split function helps in string manipulation in Python. It allows you to split up a string into different substrings. It returns a list of words present in a line or a string and is separated by a delimiter string.

In this Python Tutorial, you will learn,

  • What is Split in Python?
  • Why to use the split () function in Python?
  • Syntax of split function in Python
  • How to split a string involving a delimiter?
  • How to split a string on the occurrence of any character present in string?
  • How to split a string into a list?
  • How to split string by Application of the Max Split Operator?
  • How to use split () Function using Substring in Python?
  • Importance of split () function in Python

Why to use the split () function in Python?

Here are reasons for using Python split () function:

  • A Python program may require the manipulation of strings.
  • When you want to break large length strings or a line to many substrings or smaller strings.
  • The function works even if there is no separator or there is the presence of whitespaces. It provides some output, or it breaks the string splits.
  • The split function shares substrings in the form of elements of the string.
  • The function scans the line/string for the presence of a default separator that is passed as a parameter.
  • A split function helps in effective string management in Python code.

Syntax of split function in Python

A split function in Python provides a list of words in each line or a string. Such strings are always separated by a delimiter string. It provides one or more substrings from the main strings. The syntax of the split is composed of a function with two parameters termed separator and max with a return value.

The syntax of the split function is provided below: –

Syntax:

string. split (separator, max)

The following parameters of the function are described as follows:-

  • Separator: A separator is defined to identify the delimiter. With the use of a separator, the split function splits the main string into multiple substrings.
  • Maxsplit parameter: This is a number that is passed into the function to split the string basis into the maximum number of occurrences.
  • Return: The split function returns back to the list of strings after breaking or splitting the main string.

How to split a string involving a delimiter?

A delimiter can be defined as a string that occurs in the form of a space, comma, semicolon, colon, and any other character. The function outputs the starting and ending of the character string, word, or data item.

Let us consider the below Python example for the usage of split function with the involvement of a delimiter.

Python code:

text = "Hello World Guru99"
# splits at space
print(text.split())
text = "Hello,World, Guru99"
# splits at ','
print(text.split(", "))
text = "Hello,World:, Guru99"
# Splits at ':'
print(text.split(":"))

Output:

['Hello', 'World', 'Guru99']
['Hello,World', 'Guru99']
['Hello,World', ', Guru99']

In the above code, the string is separated using delimiters colon, space, and comma.

How to split a string on the occurrence of any character present in the string?

A split function in Python can be used in numerous ways. One such scenario could be the usage of split function to the occurrence of a character as a delimiter. A split function can be used to identify a character that could be used in place or substitute to a delimiter present in the main string to split a string.

Let us consider the below example for the usage of split function with the involvement of a character being used as a delimiter in the given string.

Python code:

text='Hello World Guru99'
#splits with character occurrence
print(text.split("l"))

Output:

['He', '', 'o Wor', 'd Guru99']

In the above code, the string is separated on the first occurrence of a character used as delimiters. Here the defined character is ‘l’.

How to split the string into a list?

A split function can also be used to split a string to be printed in the form of a list. Here, you can use loop print as a string into different substrings in the form of a list.

The following example shows the usage of the split function.

Python code:

text='Hello World Guru99'
#splits with string in the form of list
list_1 = text.split()
for st in list_1:
    print(st)

Output:

Hello
World
Guru99

In the above code, the string is separated and then listed one after another with the help of a loop.

How to split string by Application of the Max Split Operator?

A split function also provides an option of splitting the string into a maximum number of times. If the value is not initialized, then it gives -1 as the default value.

Let us consider the below example for the usage of the split function.

Python code:

text='Hello World Guru99'
split_1 = text.split(',',2)
print(split_1)
text='Hello World Guru99'
split_1 = text.split(',',4)
print(split_1)

Output:

['Hello World Guru99']
['Hello World Guru99']

In the above Python code, the string is separated on the basis of the maximum number of instances.

How to use split () Function using Substring in Python?

A substring can be defined as the string that is present within the main string. A split function can be used to split the main string-based on the substring present in that string.

Let us consider the below example for the usage of the split function.

Python code:

text='Hello World Guru 99'
split_1 = text.split('Guru',2)
print(split_1[1])
text='Hello World Guru99'
split_1 = text.split('Guru',2)
print(split_1[0])

Output:

99
Hello World

In the above code, there are two substrings named 99, and the Hello world string is separated on the basis of the substring ‘Guru’ as present in the main string ‘Hello world Guru 99’.

Importance of split () function in Python

A split function can be a very handy Python function when working on a text mining project. A split function can be used in several customizable forms. It could split strings on the initialization of the delimiter. Even if the delimiter is not present, then also the split function can be used to divide a string based on white spaces present in the string.

A split function also splits a string in the presence of multiple delimiters. This shows how dynamic and critical split function in Python is.

Summary:

  • The split function helps in string manipulation in Python.
  • A split function is composed of a specified separator and max parameter.
  • A split function can be used to split strings with the help of a delimiter
  • A split function can be used to split strings with the help of the occurrence of a character.
  • A split function can be used to split strings in the form of a list.

How do you cut a string into a list in Python?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

How do you split a string into a list of integers in Python?

To split a string into a list of integers:.
Use the str. split() method to split the string into a list of strings..
Use the map() function to convert each string into an integer..
Use the list() class to convert the map object to a list..

Can a string slice expression be used on a list?

So far, we have shown examples of lists ( list type), but slices can be used with other sequence objects such as strings str and tuples tuple as well. However, str and tuple are immutable, so new values cannot be assigned.

Can you slice a string in Python?

Python string supports slicing to create substring. Note that Python string is immutable, slicing creates a new substring from the source string and original string remains unchanged.