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...
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...
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:        ...
Finish this Code! We want to open a file called "names.txt" and display all of the...
Finish this Code! We want to open a file called "names.txt" and display all of the lines of the file as a list. Open the file in "reading" mode Use readlines to store the file into a list Use list indexing to display the 3rd item in the list This step is done for you! Close the file When you are finished, the output should match Desired Output. Notice: due to limitations with readine files in an online environment, the...
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...
#Write a function called has_a_vowel. has_a_vowel should have #one parameter, a string. It should return True...
#Write a function called has_a_vowel. has_a_vowel should have #one parameter, a string. It should return True if the string #has any vowels in it, False if it does not. def has_a_vowel(a_str): print("Starting...") for letter in a_str: print("Checking", letter) if letter in "aeiou": print(letter, "is a vowel, returning True") return True else: print(letter, "is not a vowel, returning False") return False print("Done!")       #Below are some lines of code that will test your function. #You can change the value of...
Bash script Suppose you are working as a prof or TA and your students have an...
Bash script Suppose you are working as a prof or TA and your students have an assignment which requires them each to hand in exactly one file. You've got a directory with all of the submitted files in it, and no other files. You're also lucky enough to have a script that will do all the work of marking a submission; it will take the name of a submission file as its only parameter and will print out a single...
Assume you have two dices: one has 6 faces with letters from A to B (i.e.,...
Assume you have two dices: one has 6 faces with letters from A to B (i.e., A, …, F) and the other has 20 faces with numbers from 1 to 20 (i.e., 1, …, 20)? We assume that the number dice is even, meaning that the number dice gives a number from 1 to 20 with the probability 1/20 of each. However, the letter dice is not even. The letter dice always gives you a letter from A to F...