Question

Assume you have a file called letters.txt containing all the lowercase letters of the alphabet in...

Assume you have a file called letters.txt containing all the lowercase letters of the alphabet in order one letter per line. What would be printed out by the following code?

f = open("letters.txt")
info = f.readline()
print(len(info))
f.close()
f = open("letters.txt")
info = f.readline()
info = f.readline()
print(info[0])
f.close()
f = open("letters.txt")
info = f.readline()
f.close()
f = open("letters.txt")
info = f.readline()
print(info[0])
f.close()

Homework Answers

Answer #1
f = open("letters.txt")
info = f.readline()
print(len(info))
f.close()

Python file readline() method reads one entire line in a file including the newline character('\n').

So, here the output of print() statement will be the length of characters read from the first line, i.e., 2(which is the single alphabet read and '\n' symbol).

f = open("letters.txt")
info = f.readline()
info = f.readline()
print(info[0])
f.close()

Here, the readline() method is called twice. For first call, info variable stores 'q\n' and for second call, info variable stores 'w\n'. So, the recent value stored will be used and info[0] gives the ouput as 'w'.

f = open("letters.txt")
info = f.readline()
f.close()

Here, the required file is simply opened and read and the 1st line data is stored in info variable.

f = open("letters.txt")
info = f.readline()
print(info[0])
f.close()

Here, the 1st line data is stored, i.e., 'q\n'. And, info[0] gives output 'q'.

letters.text sample file: -

q
w
e
r
t
y
u
i
o
p
a
s
d
f
g
h
j
k
l
m
n
b
v
c
x
z
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling to check if the file exists before attempting to open it. Read and print each string to the console. Next modify each word such at the first letter in each word is uppercase and all other letters are lowercase. Display the modified word on the console. Creating a text file: Open up Notepad and type in the following words. Save the file as collectionOfWords.txt...
Project File Processing. Write a program that will read in from input file one line at...
Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces,...
1. Assume there was a valid file called cards.txt in the current directory containing 5 valid...
1. Assume there was a valid file called cards.txt in the current directory containing 5 valid int values, each on its own line. It is needed to write the function that reads all values from file and then prints out: "lucky 7!" to the screen for every 7 it sees. Use the signature: void printSevens();
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Give a right-linear grammar to generate each of the following: (a) All identifiers of lowercase letters...
Give a right-linear grammar to generate each of the following: (a) All identifiers of lowercase letters that either begin or end in a (or both). (b) The set of all integers and real numbers in decimal notation. No leading zeros should be generated and real numbers must have at least one digit on both sides of the decimal point. For instance, 3, +3, -3, 0, +0, -0, 0.00328, -100.46, and +100.000 are in the language, but 100., +, -, 003...
Writing a program in Python that reads a text file and organizes the words in the...
Writing a program in Python that reads a text file and organizes the words in the file into a list without repeating words and in all lowercase. Here is what I have #This program takes a user input file name and returns each word in a list #and how many different words are in the program. while True:   #While loop to loop program     words = 0     #list1 = ['Programmers','add','an','operating','system','and','set','of','applications','to','the','hardware',          # 'we','end','up','with','a','Personal','Digital','Assistant','that','is','quite','helpful','capable',           #'helping','us','do','many','different','things']        try:        ...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
Python #Write a function called are_anagrams. The function should #have two parameters, a pair of strings....
Python #Write a function called are_anagrams. The function should #have two parameters, a pair of strings. The function should #return True if the strings are anagrams of one another, #False if they are not. # #Two strings are considered anagrams if they have only the #same letters, as well as the same count of each letter. For #this problem, you should ignore spaces and capitalization. # #So, for us: "Elvis" and "Lives" would be considered #anagrams. So would "Eleven plus...
(UNIX/LInux) Please download the file from course documents to do the following exercises after going through...
(UNIX/LInux) Please download the file from course documents to do the following exercises after going through the awk handout: 1. Print all the phone numbers. 2. Print Dan's phone number. 3. Print Susan's name and phone number. 4. Print all last names beginning with D. 5. Print all first names beginning with either a C or E. 6. Print all first names containing only four characters. 7. Print all first names of all those in the 916 area code. 8....
There is a simple encryption scheme that is called “Caesar Cipher”. In a “Caesar Cipher”, the...
There is a simple encryption scheme that is called “Caesar Cipher”. In a “Caesar Cipher”, the letters in a message are replaced by the letters of a “shifted” alphabet. A “Caesar Cipher” encodes a message by shifting each letter in a message by a constant amount of k. If k is 5, A becomes F, B becomes G, C becomes H and so on. Let's use Queue to encode and decode the encrypt message. Set k as -5 and decipher...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT