Python Code

Split a String In Python : 2 Methods and Code Tweaks

Strings in python defined inside as the single quote ‘ ‘ and double quote ” “. For example Data Science Learner or Data Science Learner“. Let’s know how to split a string in different ways. Split a string means, converting strings to lists.

How to Split a String In Python?

Method 1

The text is input by the user.

text = input("Enter the text:") #input by the user

For splitting the strings we will use split() method

print(text.split())

Full Code

text = input("Enter the text:") # input by the user
print(text.split()) # display the list

Output

The output is the list of the input text.

Enter the text:My name is John. I am 26 years old.
['My', 'name', 'is', 'John.', 'I', 'am', '26', 'years', 'old.']

 

Full Explanation

You will take the text as input by the user using text = input(“Enter the text:”).  After that, you will use the split method text.split() for splitting the text strings. Inside the split() method, there are no argument values, therefore, python interpreter will split the strings after each whitespace.

Method 2

In this method, we take input from the text file and output the text strings as the list.

Text File Contain the following information.

Data Science is already in full swing . Lots of libraries and their proper documentation is big advantage for every data scientist . As you know , Every coin has two phases .

Open the text file.

textFile = open("textFile.txt") #open the text file

Read the text file.

myFile = textFile.read() # read the text file

Split the text File.

print(myFile.split())

Full Code

textFile = open("textFile.txt") #open the text file
myFile = textFile.read() # read the text file
print(myFile.split())

Output

['Data', 'Science', 'is', 'already', 'in', 'full', 'swing', '.', 'Lots', 'of', 'libraries', 'and', 'their', 'proper', 'documentation', 'is', 'big', 'advantage', 'for', 'every', 'data', 'scientist', '.', 'As', 'you', 'know', ',', 'Every', 'coin', 'has', 'two', 'phases', '.']

Full Explanation

Suppose you have a text file containing some sentences inside it. The statement textFile = open(“textFile.txt”) will open the text file . Make sure you type the exact filename of the textfile, otherwise, it will give the following error.

FileNotFoundError: [Errno 2] No such file or directory: 'textFile2.txt'

After opening the file, you will now read the text file and store it into the variable name myFile. Now we can use the split() method on myFile. When you run the programme, you will get the list containing all the words excluding whitespaces.

Other Tweaks and Examples

1. How to Split the string after each sentence?

You will use the split( ‘, ‘) function for split the sentence.

myFile.split('.')

Full Code

textFile = open("textFile.txt") #open the text file
myFile = textFile.read() # read the text file
print(myFile.split('.'))

Output

['Data Science is already in full swing ', ' Lots of libraries and their proper documentation is the big advantage for every data scientist ', ' As you know, Every coin has two phases ', '']

In the same way, you can split the strings according to string arguments inside the split(‘String name or pattern’).

2. Count the occurrences of a specific word or How many time a particular word repeats?

Suppose you have a large text file. Your task is to count how many time a particular word has been repeated.

Let’s define the function countWordList()

def countWordList(list,findWord):
    splitList = [] #empty list for storing splitted list
    count=0 # intitalize the counter
    splitFile = list.split(" ") # split the list
    for i in range(0,len(splitFile)): # traverse the list
        if(findWord ==splitFile[i]): # match the word 
            count = count+1 # increase the counter by 1
    return count

Open and Read the text file.

textFile = open("textFile.txt") #open the text file
myFile = textFile.read() # read the text file
# string to be count
word = " machine "

Call the Function

print(countWordList(myFile,word))

Full Code

textFile = open("textFile.txt") #open the text file
myFile = textFile.read() # read the text file
# string to be count
word = " internet "

def countWordList(list,findWord):
    splitList = [] #empty list for storing splitted list
    count=0 # intitalize the counter
    splitFile = list.split(" ") # split the list
    for i in range(0,len(splitFile)): # traverse the list
        if(findWord ==splitFile[i]): # match the word 
            count = count+1 # increase the counter by 1
    return count


print(countWordList(myFile,word)) #Function call

Output

10

Download the textFileText File

Full Explanation

You have defined the function countWordList(list, findWord) containing two parameters list and findWord ( A particular word you want to count the occurrences). Inside the function, you will create an empty list (splitList =[] ) and initialize the counter to 0. After that list will be split and store inside the empty list ( splitList). Then you will traverse the entire list to count the occurrences of a particular word (“machine” ).

The myFile and word = “machine” will be passed as the arguments to the defined function countWordList(myFile, word). This will return the count of occurrences of the particular word.

 

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner