Saving Data to a Text File - Python Programming Exercise

In this exercise, you will develop a Python program to collect multiple sentences from the user (continuing until the user presses Enter without typing anything) and save them into a file named "sentences.txt". This exercise is perfect for practicing file handling, loops, and user input in Python. By implementing this program, you will gain hands-on experience in handling file operations, loops, and user input in Python. This exercise not only reinforces your understanding of file handling but also helps you develop efficient coding practices for managing user interactions.

 Category

Managing Files

 Exercise

Saving Data To A Text File

 Objective

Develop a Python program to collect multiple sentences from the user (continuing until the user presses Enter without typing anything) and save them into a file named "sentences.txt".

 Example Python Exercise

 Copy Python Code
def collect_sentences():
    """
    Collects multiple sentences from the user and saves them into a file.
    Continues until the user presses Enter without typing anything.
    """
    sentences = []
    
    print("Enter sentences. Press Enter without typing anything to finish.")
    
    while True:
        sentence = input("Enter a sentence: ")
        
        # If the user presses Enter without typing anything, stop collecting sentences
        if not sentence:
            break
        
        # Add the sentence to the list
        sentences.append(sentence)
    
    # Save the sentences to a file
    with open("sentences.txt", "w") as file:
        for sentence in sentences:
            file.write(sentence + "\n")
    
    print(f"\n{len(sentences)} sentences were saved to 'sentences.txt'.")


if __name__ == "__main__":
    collect_sentences()

 Output

Enter sentences. Press Enter without typing anything to finish.
Enter a sentence: Hello, this is the first sentence.
Enter a sentence: This is the second sentence.
Enter a sentence: Here's another one.
Enter a sentence: 
3 sentences were saved to 'sentences.txt'.

 Share this Python Exercise

 More Python Programming Exercises of Managing Files

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Adding Content to a Text File

    In this exercise, you will develop a Python program that prompts the user to input multiple sentences, stopping when they press Enter without typing anything. This ...

  •  Show File Data

    In this exercise, you will develop a Python program to read and display the contents of a text file. This exercise is perfect for practicing file handling, com...

  •  TextToHTML with File Integration

    In this exercise, you will develop a Python program to enhance the TextToHTML class by adding the ability to save its results into a text file. This exercise i...

  •  Log Handler

    In this exercise, you will develop a Python program with a class called Logger, which includes a static method called log. This exercise is perfect for practic...

  •  More

    In this exercise, you will develop a Python program that mimics the behavior of the Unix "more" command. This exercise is perfect for practicing file handling,...

  •  Text Modifier

    In this exercise, you will develop a Python program that reads a text file, replaces specified words, and saves the modified content into a new file. This exercise...