17. Write a code for returning a character at the specified index.
SOLUTION FOR THE ABOVE QUESTION>
SAMPLE PROGRAM OUTPUT
SOLUTION CODE FRO THE QUESTION
#prompt the user to enter a string
my_string = input("\nEnter a string: ")
#Find the length of the string
string_length = len(my_string)
#Propmt the user to enter the index of the character to access
index = int(input("\nEnter the index of the character you want to access: "))
#Check if index is less than the string length
if(index < string_length and (index > 0 or index == 0)):
print("\nThe character at index "+str(index)+" is "+my_string[index])
else:
print("\nYou have entered invalid index, please try again")
Comments
Leave a comment