Question
a) Besides the len() function, what other built-in functions can be used with strings? b) The for loop shows that string objects are iterable. What
a) Besides the len() function, what other built-in functions can be used with strings?
b) The for loop shows that string objects are iterable. What does that mean?
c).upper() and .lower() are string methods. What I a method and how is it
similar and different from a function?
d)What happens when a string is sliced? What would happen if there are two blank
spaces in the string?
d) Text files
Note: If no path is included, files will be placed in the same folder as the Python source
code.
Code:
def main():
# Block 1
namefile = open('names1.txt','w')
for count in range(5):
name = input('Enter a name: ')
namefile.write(name)
namefile.close()
namefile = open('names1.txt','r')
for name in namefile:
print(name)
namefile.close()
#=========================
#Block 2
namefile = open('names2.txt','w')
for count in range(5):
name = input('Enter a name: ')
namefile.write(name + ' ')
namefile.close()
namefile = open('names2.txt','r')
for name in namefile:
print(name)
namefile.close()
#========================
#block 3
namefile = open('names2.txt','w')
for count in range(5):
name = input('Enter a name: ')
namefile.write(name + ' ')
namefile.close()
namefile = open('names2.txt','r')
for name in namefile:
print(name.rstrip())
namefile.close()
#=======================
#Block 4
numfile = open('numbers.txt','w')
for count in range(5):
int_num = int(input('Enter an integer value: '))
numfile.write(str(int_num) + ' ')
numfile.close()
numfile = open('numbers.txt','r')
total = 0
for int_num in numfile:
print(int_num.rstrip())
total += int_num
numfile.close()
#=======================================
Block 5
numfile = open('numbers.txt','r')
total = 0
for int_num in numfile:
int_num = int(int_num)
print(int_num)
total += int_num
print('The total of all numbers in the file is:', total)
numfile.close()
main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started