0% found this document useful (0 votes)
6 views3 pages

Program 8

The document describes a program that counts the frequency of words in a string using a dictionary. It explains the use of the split() method to divide a string into words and details the steps to implement the program, including input handling and output formatting. Example cases demonstrate the program's functionality with different input strings.

Uploaded by

newshunt535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Program 8

The document describes a program that counts the frequency of words in a string using a dictionary. It explains the use of the split() method to divide a string into words and details the steps to implement the program, including input handling and output formatting. Example cases demonstrate the program's functionality with different input strings.

Uploaded by

newshunt535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical No.

TITLE : Program to count the frequency of words appearing in a string using a dictionary..

Problem Description : The program takes a string and counts the frequency of words appearing
in that string using a dictionary..

Theory / Analysis

String split() Method

Example

Split a string into a list where each word is a list item:

txt = "welcome to the jungle"

x = [Link]()

print(x)

The split() method splits a string into a list.

We can specify the separator, default separator is any whitespace.

When maxsplit is specified, the list will contain the specified number of elements plus one.

Syntax

[Link](separator, maxsplit)

Parameter Values

Parameter Description

Separator : Optional. Specifies the separator to use when splitting the string. By default any
whitespace is a separator

Maxsplit : Optional. Specifies how many splits to do. Default value is -1, which is "all
occurrences"

Example

Split the string, using comma, followed by a space, as a separator:

txt = "hello, my name is Peter, I am 26 years old"

x = [Link](", ")
print(x)

Example

Use a hash character as a separator:

txt = "apple#banana#cherry#orange"

x = [Link]("#")

print(x)

Example

Split the string into a list with max 2 items:

txt = "apple#banana#cherry#orange"

# setting the maxsplit parameter to 1, will return a list with 2 elements!

x = [Link]("#", 1)

print(x)

Program Description

1. Enter a string and store it in a variable.

2. Declare a list variable and initialize it to an empty list.

3. Split the string into words and store it in the list.

4. Count the frequency of each word and store it in another list.

5. Using the zip() function, merge the lists containing the words and the word counts into a
dictionary.

3. Print the final dictionary.

4. Exit.

Program

Python Program to count the frequency of words appearing in a string using a dictionary. The
program output is also shown below.
test_string=raw_input("Enter string:")

l=[]

l=test_string.split()

wordfreq=[[Link](p) for p in l]

print(dict(zip(l,wordfreq)))

Runtime Results

Case 1:

Enter string:hello world program world test

{'test': 1, 'world': 2, 'program': 1, 'hello': 1}

Case 2:

Enter string:orange banana apple apple orange pineapple

{'orange': 2, 'pineapple': 1, 'banana': 1, 'apple': 2}

You might also like