Follow
Follow

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

str.split(separator, maxsplit)

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:

text = 'wikitechy kaashiv'
  
# Splits at space 
print(text.split()) 
  
word = 'wikitechy , kaashiv'
  
# Splits at ',' 
print(word.split(',')) 
  
word = 'wikitechy:kaashiv'
  
# Splitting at ':' 
print(word.split(':')) 
  
word = 'wikitechywikitechywikitechy'
  
# Splitting at 9 
print([word[i:i+9] for i in range(0, len(word), 9)]) 

Output:


['wikitechy', ‘kaashiv’]
['wikitechy', ‘kaashiv’]
['wikitechy', ‘kaashiv’]
 ['wikitechy', ' wikitechy', ' wikitechy']

Example 2:

word = 'html, css, cloud, php'
  
# maxsplit: 0 
print(word.split(', ', 0)) 
  
# maxsplit: 4 
print(word.split(', ', 4)) 
  
# maxsplit: 1 
print(word.split(', ', 1)) 

Output

['html, css, cloud, php']
['html’, ‘css’, ‘cloud’, ‘php']
['html’, ‘css, cloud, php'] 
Newsletter
Join Design Community
Get the latest updates, creative tips, and exclusive resources straight to your inbox. Let’s explore the future of design and innovation together.