Split String in Python

Опубликовано: 16 Октябрь 2024
на канале: linuxhint
11,804
85

In this video, we have explained string splitting in python. When a string of multiple words is divided into the specific number of words based on a particular separator then it is called string splitting. Most of the programming languages use the split() method to divide a string into multiple words. The return type of this method is an array for many standard programming languages. the split() method is used in Python also to divide a string into words and it returns a list of words based on the separator. .
We have explained the string splitting in python by examples.
Following is the code that is used in this video for demonstration
Example-1: Split string based on space
text ="Welcome to the linuxhint"
print("The string is spliting on the basis on white space")
print(text.split())
Example-2: Split string based on comma
country="USA,UK,France, Russia, Parkistan, India"
listCountry=country.split(',')
for i in range (0,len(listCountry)):
print(listCountry[i])
Example-3: Split string based on the specific word
proglang="Bash and c++ and php and python"
langlist=proglang.split("and")
for i in range (0,len(langlist)):
print(lanlist[i])
Example-4: Split string based on the limit (maxsplit)
person="John:student:MIT:7thsmeseter:[email protected]"
val1=person.split(":",2)
for i in range (0,len(val1)):
print(val1[i])