F.E. PPS Unit 3 Practical 8 solution

Practical 8: Program that takes a multiline string as input and uses the split() function to separate the words.

Problem Statement

Write a Python program that takes a multiline string as input and uses the split() function to separate the words.
The program should:

  • Accept a multiline string from the user.
  • Split the string into individual words using split().
  • Display the list of words.

Solution Code

				
					# Program to use split() to split a multiline string

# Taking a multiline string input from the user
print("Enter a multiline string (press Enter twice to stop):")
lines = []
while True:
    line = input()
    if line == "":  # Stop input when an empty line is entered
        break
    lines.append(line)

# Joining lines to form a complete multiline string
multiline_string = " ".join(lines)

# Splitting the multiline string into words
words = multiline_string.split()

# Displaying the list of words
print("List of words:", words)


				
			

Expected Outcome

  • The program allows the user to enter multiple lines of text.
  • When the user presses Enter twice, the input stops.
  • The program then splits the entire text into words and displays them as a list.

Example output:

				
					Enter a multiline string (press Enter twice to stop):
There are man people
in the world who
are living all over the globe
List of words: ['There', 'are', 'man', 'people', 'in', 'the', 'world', 'who', 'are', 'living', 'all', 'over', 'the', 'globe']
Process finished with exit code 0
				
			

Explanation

Below is the explanation of F.E. PPS Unit 3 Practical 8 solution where we have written Python program that takes a multiline string as input and uses the split() function to separate the words.

  1. Taking Multiline Input from the User:
    • The user enters multiple lines until they press Enter on an empty line.
    • Each line is stored in a list (lines[]).
  2. Joining Lines to Form a Complete String:
    • multiline_string = ” “.join(lines) → Joins the lines into a single string with spaces.
  3. Splitting the String into Words:
    • split() separates the string into individual words based on spaces.
    • The resulting words are stored in a list.
  4. Displaying the List of Words:
    • The list of words is printed.

Key Concepts Learned

  • Multiline Input Handling:
    • Using a loop to take multiple lines of text.
  • Joining Strings Using join()
    • Combines multiple strings into one.
  • Using split() to Tokenize a String
    • split() separates words based on spaces by default.
Tech Amplifier Final Logo