Python program to Reverse a single line of a text file Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Given a text file. The task is to reverse a single line of user's choice from a given text file and update the already existing file. Examples: Input: Hello Geeks for geeks! User choice = 1 Output: Hello Geeks geeks! for Input: This is a geek Welcome to GeeksforGeeks GeeksforGeeks is a computer science portal User choice = 0 Output: geek a is This Welcome to GeeksforGeeks GeeksforGeeks is a computer science portal Implementation: Let's suppose the text file looks like this - Python3 # Open file in read mode f = open('GFG.txt', 'r') # Read the content of the # file and store it in a list lines = f.readlines() # Close file f.close() # User's choice choice = 1 # Split the line into words line = lines[choice].split() # line is reversed Reversed = " ".join(line[::-1]) # Updating the content of the # file lines.pop(choice) lines.insert(choice, Reversed) # Open file in write mode u = open('GFG.txt', 'w') # Write the new content in file # and note, it is overwritten u.writelines(lines) u.close() Output: Time complexity: O(n), where n is the number of lines in the file. Auxiliary space: O(n), where n is the number of lines in the file. Comment More infoAdvertise with us Next Article Python program to Reverse a single line of a text file R riasehgal1999 Follow Improve Article Tags : Python Python Programs python-file-handling Python file-handling-programs Practice Tags : python Similar Reads Python Program to Replace Text in a File In this article, we are going to replace Text in a File using Python. Replacing Text could be either erasing the entire content of the file and replacing it with new text or it could mean modifying only specific words or sentences within the existing text.Method 1: Removing all text and write new te 3 min read Python Program to Reverse the Content of a File using Stack Given a file, the task is to change the content in reverse order using Stack, as well as store the lines of that file in reverse order in Python. Examples: Input: 1 2 3 4 5 Output: 5 4 3 2 1 Approach to Python Program to Reverse a Stack Using Recursion Create an empty stack. One by one push every l 2 min read Python program to reverse the content of a file and store it in another file Given a text file. The task is to reverse as well as stores the content from an input file to an output file. This reversing can be performed in two types.  Full reversing: In this type of reversing all the content gets reversed.  Word to word reversing: In this kind of reversing the last word comes 2 min read Python Program for Reverse of a Number Using Type Casting Reversing a number is a common problem-solving exercise in programming, often used to teach the basics of algorithms and data manipulation. In Python, reversing a number can be efficiently achieved using type casting.Example:Input: 12345Output: 54321Input: -12345Output: -54321Input: 123.45Output: 54 4 min read Python Program to Reverse a Number We are given a number and our task is to reverse its digits. For example, if the input is 12345 then the output should be 54321. In this article, we will explore various techniques for reversing a number in Python. Using String SlicingIn this example, the Python code reverses a given number by conve 3 min read Like