Python Split String By Character – Split String Using split() method

Hi everyone, in this Python Split String By Character tutorial, we will learn about how to split a string in python. Splitting string means breaking a given string into list of strings. Splitting string is a very common operation, especially in text based environment like – World Wide Web or operating in a text file. Python provides some string method for splitting strings. Let’s took a look how they works.

Python Split String By Character Tutorial – Getting Started With Examples

When splitting string, it takes one string and break it into two or more lists of substrings. Some time we may need to break a large string into smaller strings. For splitting string python provides a function called split().

What Is split() function ?

  • split() method breaks a given string by a specified separator and return a list of strings.

Syntax 

  • Separator is a delimiter at which the string splits. If the separator is not specified then the separator will be any whitespace. This is optional.
  • Maxsplit is the maximum number of splits.The default value of maxsplit is -1, meaning, no limit on the number of splits. This is optional.

Splitting String By Whitespace

If we don’t specify any separator split() method takes any whitespace as separator.

Let’s understand it with an example.

  • This code will split string at whitespace.

So the output of this code is –

Python Split String By Character
Python Split String By Character

In the output, you can see that the string is broken up into a list of strings.

Splitting String By Colon ‘ : ‘

Now we will use : as a separator. Let’s see what happend.

  • This code will split string at : .

And the output is –

Python Split String By Character
Python Split String By Character

You can see that the colon(:) has been removed and string is splitted.

Splitting String By Hash Tag ‘ # ‘

Now we will use # as a separator, so for this the code is following.

  • This code will split string at #.

Let’s see the result 

All the # has been removed and string is splitted into list of substrings.

Python Split String By Character
Python Split String By Character

Splitting String By Comma ‘ , ‘

Now let’s see how to split string by comma. So write the following code.

  • This code will split string at , .

Result 

Python Split String By Character
Python Split String By Character

Splitting String By Position

Now if you want to split a string at particular position. For example string is “PythonIsProgrammingLanguage” , and you want to split this string at 4th position. So the code for this will be as follows –

  • This code will split string at 4th position.

Result

Python Split String By Character
Python Split String By Character

You can see the string is broken up from 4th position and splitted into list of substrings.

Splitting String By  A Particular Characters

Now we will split string by a particular character. Let’s take an example “Python Is Programming Language”, and now you have to split this string at character P . So code for this is like below.

  • Here we have used P as a separator, so the result is as follows.
Python Split String By Character
Python Split String By Character
  • All the P character has been removed from this string but whites paces are still in there because separator is a character.

Splitting String By Special Characters

Now we will see how to split string by special characters like \n, \t and so on. Let’s implement it in examples.

  • Here i am taking a special character \t  i.e., tab.
  • This code will split string at \t .
  • So see the result.
Python Split String By Character
Python Split String By Character

Splitting String By Maxsplit

Now we will see splitting string by maxsplit. It is a number, which tells us to split the string into maximum of provided number of times. So let’s move towards its implementation.

Result

Python Split String By Character
Python Split String By Character

 

Now we will take an another example in which we will specify both separator as well as maxsplit.

And the output of this example is –

Python Split String By Character
Python Split String By Character
  • We can see that when value of maxsplit is 4 then string is splitted into a list of substrings where first four string is splitted at # and remaining are same.
  • When value of maxsplit is 1 then string is splitted into a list of substrings where only first  string is splitted at # and remaining are same.
  • When maxsplit is 0 then no splitting occur because the value is zero.

Recommended Posts

So friends, now i am wrapping up Python Split String By Character tutorial. If you have any query then please let me know this. And please share this post with your friends and suggest them to visit simplified python for best python tutorials.  Thanks Everyone.

Leave a Comment