loading...

how to split a string in python?

pakainfocom profile image pakainfo ・1 min read

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

[caption id="attachment_12690" align="aligncenter" width="1024"]split, turn characters, python split, delimiter, split python, split string python, python split python split string into list[/caption]

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']

You can visit my Article with More Examples : click here

Posted on by:

pakainfocom profile

pakainfo

@pakainfocom

My name is Angular King. I'm a full-stack developer, entrepreneur and owner of Pakainfo. I live in India and I love to write

Discussion

pic
Editor guide
 

You can use markdown in your articles for code. that will look better

example:-

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

I hope it will help you.