how to split a string in python? | python split string into list

Today, We want to share with you python split string into list.In this post we will show you Split a string into a list in Python, hear for python splits string into list by commas we will give you demo and example for implement.In this post, we will learn about Count Number Of Lines In A Text File In Python with an example.

Read Also:  Toopics instagram viewer

Definition and Usage & Syntax

split, turn characters, python split, delimiter, split python, split string python, python split
python split string into list

Using str.split()

I can use the simple str.split(sep=None) methods which data returns a list of the words in the data string, using sep as the main logic delimiter string.

For below example, to split the string with delimiter special char like as -, I can do:

mystring = '8-9-6'
strlen = mystring.split('-')
print(strlen)	# prints ['8', '9', '6']

If sep is not specified or is None, executes of consecutive whitespace are regarded as a single separator.

mystring = '8 9 6'
strlen = mystring.split()
print(strlen)	# prints ['8', '9', '6']

Using shlex.split()

The shlex main module defines the shlex.split(mystring) methods which data split the data string mystring using simple logic here available shell-like syntax.

import shlex

mystring = '8 9 6'
strlen = shlex.split(mystring)
print(strlen)	# prints ['8', '9', '6']

Python String split() Method

mystring = "welcome to the"

strlen = mystring.split()



['welcome', 'to', 'the', '']

Split the string into a list with max 2 items:

mystring = "pakainfo#infinityknow#jaydeep#patel"

results = mystring.split("#", 1)



['pakainfo', 'infinityknow#jaydeep#patel']

I hope you get an idea about python split string into list.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Table with CSS Styles using Angular Example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Form Data Binding using Angular Example
  2. Read Also:  Removing index.php From Codeigniter URL htaccess
  3. Read Also:  how to check and know my jio Mobile number From MyJio App or using Jio4GVoice App?
  4. Read Also:  ANGULARJS PHP MySQL Inline CRUD Example Tutorial From Scratch
  5. Read Also:  PHP String Explode and Implode Function
  6. Read Also:  How to Convert String to array in php Examples
  7. Read Also:  Laravel 6 Get Config Variable Example - PHP
  8. Read Also:  How to Create Service in Angular 9 Example
  9. Read Also:  CodeIgniter Autocomplete Multiple input textbox with Database
  10. Read Also:  Laravel 6 get last Insert id from query builder