Question
a) A Python for loop iterates? Describe what that means using the first two for loops in the example code. b) What does the range()
a) A Python for loop iterates? Describe what that means using the first two for
loops in the example code.
b) What does the range() function do? What happens when one, two, and three
arguments are used with the range function?
c) In most languages, altering the counting variable that is being used to control a
for loop in the body of the loop can cause problems and lead to the loop not
executing the desired number of times. In the last for loop in the example, the
count variable is being altered. Is this causing problems with the number of loop
repeats? Why or why not?
c) Strings
Code
def main():
string_val = input('Enter a string value: ')
#Built-in function
print('The string contains', len(string_val), 'characters')
#String iteration
print(' Each character in the string is:')
for ch in string_val:
print(ch)
#string methods
print(' THe string all upper case is:', string_val.upper())
print(' THe string all lower case is:', string_val.lower())
#string concatenation
print(' THe string concatenated is:', string_val +
string_val, end = ' ')
#string splicing
fullname = input('Enter your first and last name: ')
blank_space = fullname.index(' ')
print(' Your first name is:', fullname[0:blank_space])
print(' Your last name is:', fullname[blank_space+1:])
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