Python String split()

Python split() method splits the string into a comma-separated list.

Python String

  • Python string is that the collection of the characters surrounded by single quotes, double quotes, or triple quotes.
  • The computer doesn’t understand the characters; internally, it stores manipulated character because of the combination of the 0’s and 1’s.
  • Each character is encoded within the ASCII or Unicode character. So we will say that Python strings also are called the collection of Unicode characters.

Python StringΒ  split()

  • Python split() method splits the string into a comma-separated list.
  • split() method returns a list of strings after breaking the given string by the specified separator.
  • This method takes two parameters and both are optional.

Syntax

[pastacode lang=”python” manual=”str.split(separator%2C%20maxsplit)” message=”” highlight=”” provider=”manual”/]

Parameters :

  • separator : This is often a delimiter. The string splits at this specified separator. If isn’t provided then any white space may be a separator.
  • maxsplit : It’s a number, which tells us to separate the string into maximum of provided number of times. If it’s not provided then there’s no limit.
  • Returns : Returns a list of strings after breaking the given string by the specified separator.

Example 1:

[pastacode lang=”python” manual=”text%20%3D%20’wikitechy%20kaashiv’%0A%20%20%0A%23%20Splits%20at%20space%20%0Aprint(text.split())%20%0A%20%20%0Aword%20%3D%20’wikitechy%20%2C%20kaashiv’%0A%20%20%0A%23%20Splits%20at%20’%2C’%20%0Aprint(word.split(‘%2C’))%20%0A%20%20%0Aword%20%3D%20’wikitechy%3Akaashiv’%0A%20%20%0A%23%20Splitting%20at%20’%3A’%20%0Aprint(word.split(‘%3A’))%20%0A%20%20%0Aword%20%3D%20’wikitechywikitechywikitechy’%0A%20%20%0A%23%20Splitting%20at%209%20%0Aprint(%5Bword%5Bi%3Ai%2B9%5D%20for%20i%20in%20range(0%2C%20len(word)%2C%209)%5D)%20%0A%0A%0A” message=”” highlight=”” provider=”manual”/]

Output:

[pastacode lang=”bash” manual=”%0A%5B’wikitechy’%2C%20%E2%80%98kaashiv%E2%80%99%5D%0A%5B’wikitechy’%2C%20%E2%80%98kaashiv%E2%80%99%5D%0A%5B’wikitechy’%2C%20%E2%80%98kaashiv%E2%80%99%5D%0A%20%5B’wikitechy’%2C%20’%20wikitechy’%2C%20’%20wikitechy’%5D%0A” message=”” highlight=”” provider=”manual”/]

Example 2:

[pastacode lang=”python” manual=”word%20%3D%20’html%2C%20css%2C%20cloud%2C%20php’%0A%20%20%0A%23%20maxsplit%3A%200%20%0Aprint(word.split(‘%2C%20’%2C%200))%20%0A%20%20%0A%23%20maxsplit%3A%204%20%0Aprint(word.split(‘%2C%20’%2C%204))%20%0A%20%20%0A%23%20maxsplit%3A%201%20%0Aprint(word.split(‘%2C%20’%2C%201))%20%0A” message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”bash” manual=”%5B’html%2C%20css%2C%20cloud%2C%20php’%5D%0A%5B’html%E2%80%99%2C%20%E2%80%98css%E2%80%99%2C%20%E2%80%98cloud%E2%80%99%2C%20%E2%80%98php’%5D%0A%5B’html%E2%80%99%2C%20%E2%80%98css%2C%20cloud%2C%20php’%5D%20%0A” message=”” highlight=”” provider=”manual”/]

Leave a Reply

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

You May Also Like

Why you choose Python ?

Definition Python is a versatile, high-level programming language known for its simplicity, readability, and broad applicability across various…
Read More

Data Types in Python

Definition Data types in Python are classifications that specify the type of value a variable holds, determining what…
Read More

Multithreading in Python

Definition: Multithreading in Python involves running multiple threads within a single process to perform concurrent tasks, allowing for…