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.
Definition and Usage & Syntax
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']
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 Pakainfo.com" strlen = mystring.split() print(strlen)
['welcome', 'to', 'the', 'Pakainfo.com']
Split the string into a list with max 2 items:
mystring = "pakainfo#infinityknow#jaydeep#patel" results = mystring.split("#", 1) print(results)
I hope you get an idea about python split string into list.
I would like to have feedback on my infinityknow.com 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.