Write a python program to accept string/sentences from the user till the user enters “END” to. Save the data in a text file and then display only those sentences which begin with an uppercase alphabet
Answers
Answer:
Python
File and Text Processing
1. File Input/Output
File Input/Ouput (IO) requires 3 steps:
Open the file for read or write or both.
Read/Write data.
Close the file to free the resouces.
Python provides built-in functions and modules to support these operations.
1.1 Opening/Closing a File
open(file, [mode='r']) -> fileObj: Open the file and return a file object. The available modes are: 'r' (read-only - default), 'w' (write - erase all contents for existing file), 'a' (append), 'r+' (read and write). You can also use 'rb', 'wb', 'ab', 'rb+' for binary mode (raw-byte) operations. You can optionally specify the text encoding via keyword parameter encoding, e.g., encoding="utf-8".
fileObj.close(): Flush and close the file stream.
1.2 Reading/Writing Text Files
The fileObj returned after the file is opened maintains a file pointer. It initially positions at the beginning of the file and advances whenever read/write operations are performed.
Reading Line/Lines from a Text File
fileObj.readline() -> str: (most commonly-used) Read next line (upto and include newline) and return a string (including newline). It returns an empty string after the end-of-file (EOF).
fileObj.readlines() -> [str]: Read all lines into a list of strings.
fileObj.read() -> str: Read the entire file into a string.
Writing Line to a Text File
fileObj.write(str) -> int: Write the given string to the file and return the number of characters written. You need to explicitly terminate the str with a '\n', if needed. The '\n' will be translated to the platform-dependent newline ('\r\n' for Windows or '\n' for Unixes/Mac OS).
Answer:
The following Python program performs the desired function:
text = input()
arr = text.split( )
for i in arr:
if(i.uppercase() == true):
print(i)
Explanation:
- We start the code by creating a variable text that will store the text input of the user.
- Then we need to split this input so that we can analyze each word of the text.
- Python gives a split function for splitting the text input.
- Then we iterate over the split words by using a for loop
- If any of the letters of the text is uppercase then we need to print it.
- For doing so, we are using the uppercase() function of the python that returns true if the word is uppercased.
- If it is true then we print the word otherwise we keep moving ahead.
#SPJ3