• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Python Split String [ Delimiter, Space, Character, Regex, Multiple Delimiters ]

November 3, 2020 by javainterviewpoint Leave a Comment

Splitting a String into separate words is one of the most common operations performed on a String. We can use the split() method of the str class to perform the split operation. In this Python Split String article, we will learn how to split string in Python based on a delimiter, comma, space, character, regex, and multiple delimiters.

Python Split String

Python Split String

 

All developers would have come across this situation, where we need to split a complete string into separate words.

Suppose if we have a string of usernames separated by a comma, we need to split them into individual username so that we can perform any operation on it, such as counting the number of users available.

>>> usernames = "Tim, Bob, Bill, Tom, Sam"

We can use the split() method to split the users based on comma delimiter. Let’s understand the split() method first before getting to the solution.

String split() method

The split() method returns a list of words in the string separated by the delimiter passed. The syntax of the split() method is

str.split(separator, maxsplit)

  • The separator acts as the delimiter, and the string gets split based on the separator. If no separator is specified, then the split() method splits the string based on whitespace
  • The maxsplit number specifies the maximum number of Split. If the maxsplit is not specified, then it is considered as -1 meaning no limit.

As a first step, let’s try to fix the issue stated above.

>>> usernames = "Tim, Bob, Bill, Tom, Sam"
>>> listOfUsers = usernames.split(',')
>>> type(listOfUsers)
<class 'list'>
>>> print("Number of Users available are "+str(len(listOfUsers)))
Number of Users available are 5

Now the listOfUsers will have all the individual users separated.

Let’s pass the maxsplit number as 2

>>> listOfUsers = usernames.split(',',2)
>>> listOfUsers
['Tim', ' Bob', ' Bill, Tom, Sam']

We can see that only 2 splits have happened ‘Tim’ and ‘Bob’ are separated, and the remaining users [Bill, Tom, Sam] are not split

Let’s try maxsplit as 3 this time

>>> listOfUsers = usernames.split(',',3)
>>> listOfUsers
['Tim', ' Bob', ' Bill', ' Tom, Sam']

Now 3 splits have happened ‘Tim’, ‘Bob’, and ‘Bill’ are separated.

Split by Space

Whenever the delimiter is not specified or is null, then the string will be split using the Space / Whitespace as a delimiter.

>>> msg = "Welcome to Java Interview Point"
>>> words = msg.split()
>>> words
['Welcome', 'to', 'Java ', 'Interview', 'Point']

Whenever there are multiple consecutive whitespaces, it is considered as a single separator. In the below snippet, we have multiple spaces between each word.

>>> msg = "Welcome      to     Java Interview Point"
>>> words = msg.split()
>>> words
['Welcome', 'to', 'Java', 'Interview', 'Point']

Whitespace includes newline (\n) and tab space (\t) characters as well. So if the string contains \n or \t, it is considered as space only.

>>> msg = "Welcome\nto\tJava Interview Point"
>>> words = msg.split()
>>> words
['Welcome', 'to', 'Java','Interview','Point']

Python Split String by Character

There are three different ways by which we can split a string into a list of characters.

  1. Using List slice assignment
  2. By Passing the String to the list constructor
  3. With For Loop

1.Using List slice assignment

Slice Assignment is a special syntax for Lists, using which we can alter the contents of the lists. Let’s split the string into characters using List slice assignment

>>> msg = "Welcome"
>>> chars = []
>>> chars[:] = msg
>>> chars
['W', 'e', 'l', 'c', 'o', 'm', 'e']

By specifying chars[:] on the left side of the = operator, we are telling Python to use Slice Assignment.

2. By Passing the String to list constructor

The list() constructor takes a single iterable argument which can be a sequence or any iterator object

We just need to pass the sequence (string) to the list() constructor, as it is a type of iterable, the list() constructors splits them into individual characters.

>>> msg = "Welcome"
>>> chars = list(msg)
>>> chars
['W', 'e', 'l', 'c', 'o', 'm', 'e']

3. With For Loop

This is a kind of manual approach where we take each character of the string and append it to the list.

>>> for char in msg:
       chars.append(char)
>>> chars
['W', 'e', 'l', 'c', 'o', 'm', 'e']

Python String Split by regex

We can use a regular expression to split a string, we need to import re module and use the split() method of it.

The syntax is

re.split(pattern, string, maxsplit, flags)

For example, let’s take a string separated by underscore ‘_’, we just need to pass the delimiter inside the square brackets []

>>> import re
>>> message = "Welcome_to_Javainterview_Point"
>>> words = re.split('[_]', message)
>>> words
['Welcome', 'to', 'Javainterview', 'Point']

Let’s try with the maxsplit as 2

>>> words = re.split('[_]', message,2)
>>> words
['Welcome', 'to', 'Javainterview_Point']

The flags parameter allows you to modify the way the Regular expression works. We can use flags in two ways, either the long name or short name.
For example, if we want the regex to ignore the cases, then we can use the flag IGNORECASE, or I.

>>> numbers = "1234aaaa567BBB890ccc987"
>>> numberList = re.split('[a-c]+', numbers, flags = re.IGNORECASE)
>>> numberList
['1234', '567', '890', '987']

Splitting with Multiple Delimiters

We can also pass multiple delimiters to the re.split() method. Let’s try to split the string based on the semicolon, comma, and space as delimiters

>>> text = "one,two;three    four,five   six"
>>> numbers = re.split('[;,\s]+', text)
>>> numbers
['one', 'two', 'three', 'four', 'five', 'six']

rsplit() method – Split from right

rsplit() is similar to the split() method of the str class, except for the fact that it starts splitting the string from the right end.

>>> usernames = "Tim, Bob, Bill, Tom, Sam"
>>> users = usernames.rsplit(',')
>>> users
['Tim', ' Bob', ' Bill', ' Tom', ' Sam']

Looks the same, right? We can see the difference only when we give the maxsplit argument.

>>> users = usernames.rsplit(',', 2)
>>> users
['Tim, Bob, Bill', ' Tom', ' Sam']
>>> users = usernames.rsplit(',', 3)
>>> users
['Tim, Bob', ' Bill', ' Tom', ' Sam']

Now we can see that it starts splitting the words from the right.

splitlines() method – Splitting String by line break

The splitlines() method splits the string based on the line break characters such as \n, \r, \r\n, etc..

>>> msg = "Welcome\nTo\rJavaInterview\r\nPoint"
>>> words = msg.splitlines()
>>> words
['Welcome', 'To', 'JavaInterview', 'Point']

I hope, I have covered most of the ways to split a string in Python. Feel free to drop a comment if you found anything missing or needs to be added.

Happy Learning!!

Filed Under: Python Tagged With: Character, Comma, delimiter, Multiple Delimiters, Python Split String, Python String Split, Regex, rsplit(), Space, split, split string, splitlines(), String Split

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·