How do you split print in python?

❮ String Methods

Example

Split a string into a list where each word is a list item:

txt = "welcome to the jungle"

x = txt.split[]

print[x]

Try it Yourself »

Definition and Usage

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.

Syntax

string.split[separator, maxsplit]

Parameter Values

ParameterDescription
separator Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator
maxsplit Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences"

More Examples

Example

Split the string, using comma, followed by a space, as a separator:

txt = "hello, my name is Peter, I am 26 years old"

x = txt.split[", "]

print[x]

Try it Yourself »

Example

Use a hash character as a separator:

txt = "apple#banana#cherry#orange"

x = txt.split["#"]

print[x]

Try it Yourself »

Example

Split the string into a list with max 2 items:

txt = "apple#banana#cherry#orange"

# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split["#", 1]

print[x]

Try it Yourself »

❮ String Methods


In this tutorial, we will learn about the Python String split[] method with the help of examples.

The split[] method breaks up a string at the specified separator and returns a list of strings.

Example

text = 'Python is a fun programming language'

# split the text from space print[text.split[' ']]

# Output: ['Python', 'is', 'a', 'fun', 'programming', 'language']

Syntax of String split[]

The syntax of split[] is:

str.split[separator, maxsplit]

split[] Parameters

The split[] method takes a maximum of 2 parameters:

  • separator [optional]- Delimiter at which splits occur. If not provided, the string is splitted at whitespaces.
  • maxsplit [optional] - Maximum number of splits. If not provided, there is no limit on the number of splits.

split[] Return Value

The split[] method returns a list of strings.

Example 1: How split[] works in Python?

text= 'Love thy neighbor'

# splits at space

print[text.split[]]

grocery = 'Milk, Chicken, Bread' # splits at ','

print[grocery.split[', ']]

# Splits at ':' print[grocery.split[':']]

Output

['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']

Example 2: How split[] works when maxsplit is specified?

grocery = 'Milk, Chicken, Bread, Butter'

# maxsplit: 2

print[grocery.split[', ', 2]]

# maxsplit: 1 print[grocery.split[', ', 1]] # maxsplit: 5

print[grocery.split[', ', 5]]

# maxsplit: 0 print[grocery.split[', ', 0]]

Output

['Milk', 'Chicken', 'Bread, Butter']
['Milk', 'Chicken, Bread, Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
['Milk, Chicken, Bread, Butter']

If maxsplit is specified, the list will have a maximum of maxsplit+1 items.

The split[] method in Python returns a list of the words in the string/line , separated by the delimiter string. This method will return one or more new strings. All substrings are returned in the list datatype.

Syntax

ParameterDescription
separator The is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.
maxsplit It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then there is no limit.
return The split[] breaks the string at the separator and returns a list of strings.

If no separator is defined when you call upon the function, whitespace will be used by default. In simpler terms, the separator is a defined character that will be placed between each variable. The behavior of split on an empty string depends on the value of sep. If sep is not specified, or specified as None, the result will be an empty list. If sep is specified as any string, the result will be a list containing one element which is an empty string .

Splitting String by space

The split[] method in Python without an argument splits on whitespace.

example

output

Splitting on first occurrence

In the following example, it will Split by first 2 whitespace only.

example

output

Splitting lines from a text file in Python

The following Python program reading a text file and splitting it into single words in python

example

Splitting String by newline[\n]

output

Splitting String by tab[\t]

output

Splitting String by comma[,]

output

Split string with multiple delimiters

In this case Python uses Regular Expression.

example

output

Split a string into a list

The following Python program split a string to a List.

example

output

maxsplit parameter

Split the string into a list with max 2 items

output

In the above program maxsplit is 2, the first two string are split and rest of them are in a same string.

Split a string into array of characters

output

Python split[] using substring

Extact a string after a specific substring.

In the above example, you can see the split[] function return next part of a string using a specific substring.

Here, you can see the split[] function return the previous part of the string using a specific substring.

Looking for a Python job ?

Chances are you will need to prove that you know how to work with Python. These Python Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Python Programming . Here are the top objective type sample Python Interview questions and their answers are given just below to them. These sample questions are framed by our experts team who trains for Python training to give you an idea of type of questions which may be asked in interview.

Go to... Python Interview Questions



How do you split a print statement in Python?

You cannot split a statement into multiple lines in Python by pressing Enter . Instead, use the backslash [ \ ] to indicate that a statement is continued on the next line. In the revised version of the script, a blank space and an underscore indicate that the statement that was started on line 1 is continued on line 2.

Is there a split function in Python?

The string manipulation function in Python used to break down a bigger string into several smaller strings is called the split[] function in Python. The split[] function returns the strings as a list.

How do you create a split function in Python?

How to use Split in Python.
Create an array. x = 'blue,red,green'.
Use the python split function and separator. x. split[“,”] – the comma is used as a separator. This will split the string into a string array when it finds a comma..
Result. ['blue', 'red', 'green'].

How split method works in Python?

Python String split[] Method Syntax The string splits at this specified separator. If is not provided then any white space is a separator. maxsplit : It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then the default is -1 that means there is no limit.

Chủ Đề